【发布时间】:2010-11-22 17:40:07
【问题描述】:
如何更改 UIActionSheet 按钮的颜色?
【问题讨论】:
标签: iphone cocoa-touch uikit
如何更改 UIActionSheet 按钮的颜色?
【问题讨论】:
标签: iphone cocoa-touch uikit
iOS 8 (UIAlertController)
如果您使用的是 UIAlertController,这非常简单。只需更改 UIAlertController 视图上的色调颜色即可。
[alertController.view setTintColor:[UIColor red];
iOS 7 (UIActionSheet)
我用这个简单的方法成功改变了文字颜色。
- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet {
UIColor *tintColor = [UIColor redColor];
NSArray *actionSheetButtons = actionSheet.subviews;
for (int i = 0; [actionSheetButtons count] > i; i++) {
UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i];
if([view isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton*)view;
[btn setTitleColor:tintColor forState:UIControlStateNormal];
}
}
}
确保在您调用 之后运行此
[actionSheet showInView];
如果您在 [showInView] 之前调用它,则除取消按钮之外的所有按钮都将着色。希望这对某人有帮助!
【讨论】:
我创建了 UICustomActionSheet 子类,它允许自定义 UIActionSheet 中按钮的字体、颜色和图像。对应用商店来说绝对安全,你可以在下一个链接找到这个类的代码:
https://github.com/gloomcore/UICustomActionSheet
尽情享受吧!
【讨论】:
很遗憾,如果不使用未记录的 API,则没有官方方法可以更改 UIActionSheet 上按钮的颜色。如果您将 UIActionSheet 控件子类化,您也许可以自定义它。
请参阅此示例:http://blog.corywiles.com/customizing-uiactionsheet-buttons
【讨论】:
UIActionSheet。来自文档:UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.
我们可以使用背景图片来做到这一点。我认为这是最简单的方法。
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actionsheet" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet addButtonWithTitle:@"Button 1"]; //Blue color
[actionSheet addButtonWithTitle:@"Button 2"];
[actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet addButtonWithTitle:nil];
[actionSheet setCancelButtonIndex:2];
[actionSheet setDestructiveButtonIndex:1];
[actionSheet showInView:self.view];
UIButton *button = [[actionSheet subviews] objectAtIndex:1];
UIImage *img = [button backgroundImageForState:UIControlStateHighlighted];//[UIImage imageNamed:@"alert_button.png"];
[button setBackgroundImage:img forState:UIControlStateNormal];
【讨论】:
您可以使用以下代码轻松实现它
Apple
UIActionSheetDelegate协议文档
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
for (UIView *_currentView in actionSheet.subviews)
{
if ([_currentView isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)_currentView;
[button setTitleColor:YOUR_COLOR forState:UIControlStateNormal];
}
}
}
【讨论】: