在UIActionSheet按钮更改文本颜色(Change text color in UIActi

2019-08-31 21:18发布

在我的项目,我使用UIActionSheet用于显示一些排序类型。 我想改变的动作片按钮显示的文本的颜色。 我们怎样才能改变文字颜色?

Answer 1:

的iOS 8: UIActionSheet已被弃用。 UIAlertController尊重-[UIView tintColor]所以此工程:

alertController.view.tintColor = [UIColor redColor];

更重要的是,在你的应用程序代理设置整个窗口的色调颜色:

self.window.tintColor = [UIColor redColor];

iOS的7:在你的动作片的委托,实施-willPresentActionSheet:如下:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}


Answer 2:

在iOS系统中8,这一切都不工作了。 UIActionSheet文本似乎得到window.tintColor它的颜色。



Answer 3:

这对我的作品在iOS8上

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];


Answer 4:

要是你想通过该UIActionSheet的子视图,你不应该直接设置的文本颜色UILabel ,但使用UIButton setTitleColor:forState方法来代替。 否则,初始颜色将在像UIControlEventTouchDragOutside事件例如被重新设置。

下面是做到这一点,重用JRC的代码的正确方法:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}


Answer 5:

可能是一个迟到的答案,但我想它可以帮助别人。

iOS的8:你可以简单地与风格初始化UIAlertAction:UIAlertActionStyleDestructive像这样:

UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){

   // your code in here   

}];

这将使该按钮的文本红色的默认。



Answer 6:

@jrc解决方案的工作,但是当你点击按钮titleLabel颜色变化。 试试这个,以保持相同的颜色,在所有状态。

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{     
 UIColor *customTitleColor = [UIColor greenColor];
      for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)subview;

          [button setTitleColor:customTitleColor forState:UIControlStateHighlighted];
          [button setTitleColor:customTitleColor forState:UIControlStateNormal];
          [button setTitleColor:customTitleColor forState:UIControlStateSelected];
        }
      }
}


Answer 7:

编辑

这里有一个类似的问题: 如何自定义按钮UIActionSheet?

尝试使用appearance协议[NOT WORKING]

 UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];


Answer 8:

用这个IOS 8及以上

SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
    UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
    if ([alertController isKindOfClass:[UIAlertController class]])
    {
        alertController.view.tintColor = [UIColor blueColor];
    }
}
else
{
    // use other methods for iOS 7 or older.
}


Answer 9:

Soulution的斯威夫特3。 更改所有的颜色。

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow


Answer 10:

UIAppearance可以处理这个问题:

[[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];

大部分不是通过子视图会更容易,但我没有测试在iOS 6或更早版本的这一解决方案。



文章来源: Change text color in UIActionSheet buttons