【发布时间】:2011-08-02 11:33:33
【问题描述】:
我有一个带有几个按钮的 UIAlertView。我想让某个按钮变成红色。我怎样才能做到这一点?
【问题讨论】:
标签: iphone ios4 iphone-sdk-3.0
我有一个带有几个按钮的 UIAlertView。我想让某个按钮变成红色。我怎样才能做到这一点?
【问题讨论】:
标签: iphone ios4 iphone-sdk-3.0
虽然最初的问题是如何更改 UIAlertView 按钮颜色,但后来改为 UIActionSheet,以防万一有人要更改 UIActionSheet 按钮颜色,可以使用以下 UIActionSheet 委托方法:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *aView in actionSheet.subviews) {
UIButton *aButton = (UIButton *)aView;
[aButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
【讨论】:
你不能继承UIAlertView。请查看 Apple 的此链接:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
【讨论】:
class MyAlertView: UIAlertView { init(customParams: .....) { super.init(frame: CGRect.zeroRect) setup() } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } setup() { // Setup buttons, delegates...whatever you want } // More custom logic... }
然后尝试 UIActionSheet 而不是 UIAlertView
【讨论】:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
不幸的是 UIAlerView 没有办法改变按钮的颜色。 如果您想要一个外观不同的 UIAlertView,您必须自己设计它并以模态视图的形式呈现。
【讨论】:
不幸的是,我认为您无法更改它。也许只能通过使用类别来覆盖,这绝对是苹果不鼓励的。但是,创建自定义 uialertview 非常容易。
【讨论】: