完全可定制的 iOS 7/8 风格 UIActionSheet 替代品。与 iOS 5、6、7 和 8 兼容。
默认情况下,IBActionSheet 完美地模仿了 iOS 7/8 UIActionSheet
然后您可以选择更改
除了 '按钮按下' 的效果外,action sheet 的所有元素都可以进行自定义,包括整个 action sheet、单个按钮和标题。目前的 '按钮按下' 效果包括
IBActionSheet 提供了与 UIActionSheet 相同的功能,包括
- (id)initWithTitle:(NSString *)title delegate:(id<IBActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle otherButtonTitles:(NSString *)otherTitles, ... NS_REQUIRES_NIL_TERMINATION;
- (void)showInView:(UIView *)theView;
- (NSInteger)addButtonWithTitle:(NSString *)title;
- (void)dismissWithClickedButtonIndex:
- (NSInteger)buttonIndex animated:(BOOL)animated;
- (NSInteger)numberOfButtons;
- (NSString *)buttonTitleAtIndex:(NSInteger)index;
- (BOOL)visible;
- (NSInteger)cancelButtonIndex;
- (NSInteger)destructiveButtonIndex;
要接收 IBActionSheet 的通知,只需将 <IBActionSheetDelegate>
添加到您的视图控制器的头文件中,并实现以下方法
-(void)actionSheet:(IBActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
它的工作方式与 UIActionSheet 相同,事实上,它还会从 UIActionSheet 接收通知。
您可以选择添加以下这些可选方法,它们的行为也类似于 UIActionSheet 的对应方法
-(void)actionSheet:(IBActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
-(void)actionSheet:(IBActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
接下来,进入精彩部分,要自定义 action sheet,您可以选择以下选项
// rotation
- (void)rotateToCurrentOrientation;
// effects
- (void)setButtonResponse:(IBActionSheetButtonResponse)response;
// fonts
- (void)setFont:(UIFont *)font;
- (void)setTitleFont:(UIFont *)font;
- (void)setFont:(UIFont *)font forButtonAtIndex:(NSInteger)index;
- (void)setCancelButtonFont:(UIFont *)font;
- (void)setDestructiveButtonFont:(UIFont *)font;
// standard colors
- (void)setTitleTextColor:(UIColor *)color;
- (void)setButtonTextColor:(UIColor *)color;
- (void)setTitleBackgroundColor:(UIColor *)color;
- (void)setButtonBackgroundColor:(UIColor *)color;
- (UIColor *)buttonTextColorAtIndex:(NSInteger)index;
- (UIColor *)buttonBackgroundColorAtIndex:(NSInteger)index;
- (void)setButtonTextColor:(UIColor *)color forButtonAtIndex:(NSInteger)index;
- (void)setButtonBackgroundColor:(UIColor *)color forButtonAtIndex:(NSInteger)index;
// highlight colors
- (void)setButtonHighlightBackgroundColor:(UIColor *)color;
- (void)setButtonHighlightTextColor:(UIColor *)color;
- (void)setButtonHighlightTextColor:(UIColor *)color forButtonAtIndex:(NSInteger)index;
- (void)setButtonHighlightBackgroundColor:(UIColor *)color forButtonAtIndex:(NSInteger)index;
我已经包含了超简单的示例项目,它会展示如何使用。如果您有任何问题或建议,请随时告诉我!
**注意:如果您在 iOS 7 上运行此应用程序,请在安装到运行 7.0 的设备之前,确保将部署目标更改为 7.0。否则,动画将略有偏差
在 iPad 上,它遵循 iPhone 风格的 UIActionSheet 而不是 iPad 的 UIActionSheet。我个人更喜欢这种行为,但如果有人要求,我会很乐意使其遵循 iPad 的 UIActionSheet 行为,只需告诉我!
IBActionSheet不会像UIActionSheet那样锁定方向。我还没有找到一种优雅的解决方案。你可以使用actionSheet.visible属性来自定义锁定,或者你可以在检测旋转的方法中调用
[actionSheet rotateToCurrentOrientation];
相应地调整大小。
[actionSheet showInView:self.navigationController.view];
而不是
[actionSheet showInView:self.view];
希望将来可以消除做这件事的需要。