项目更改文本颜色在UIActionSheet - iOS设备8

问题描述 投票:34回答:9

使用下面的代码来改变我在UIActionSheet添加项目的文本颜色我已经:

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

它工作在iOS7罚款,但在iOS8上面的代码不会改变项目的文本颜色。

现在的问题是如何改变,我使用UIActionSheetiOS8添加项目的文本颜色? 附加信息: 我加了断点看到从上面的代码下面几行是否得到执行与否:

UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

这些线路将无法得到执行。所以,if([subview isKindOfClass:[UIButton class]])总是falseactionSheet.subviews的所有项目。因此,我认为是UIActionSheet的视图层次结构已经改变。

ios uiactionsheet ios8 uiactionsheetdelegate
9个回答
36
投票

这里有一个简单的方法,如果你仍然想以改用UIActionSheet UIAlertController的支持老版本的IOS。

UIActionSheet实际使用UIAlertController在iOS 8的,它有一个私人财产_alertController

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.
}

对于斯威夫特下面的代码应该工作

let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in

     }

    alertAction.setValue(UIColor.red, forKey: "titleTextColor")

31
投票

要改变你需要使用UIAlertController并设置主tintColorview按钮标题颜色。

设置按钮标题颜色为黑色的实施例:

UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"How would you like to proceed?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *finish = [UIAlertAction actionWithTitle:@"Finish" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                               {
                                   [self performSelector:@selector(finish:) withObject:nil];
                               }];

UIAlertAction *playAgain = [UIAlertAction actionWithTitle:@"Play Again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                            {
                                [self performSelector:@selector(playAgain:) withObject:nil];
                            }];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                         {
                             [actionSheetController dismissViewControllerAnimated:YES completion:nil];
                         }];

[actionSheetController addAction:finish];
[actionSheetController addAction:playAgain];
[actionSheetController addAction:cancel];

//******** THIS IS THE IMPORTANT PART!!!  ***********
actionSheetController.view.tintColor = [UIColor blackColor];

[self presentViewController:actionSheetController animated:YES completion:nil];

10
投票

好吧,我遇到了同样的问题,但我想我已经找到了解决办法:

用适当的方式应该是这样的,我想它在iOS7:

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

但它不会在iOS8上由于事实ActionSheets“按钮”现在是基于UICollectionView工作。因此,一些周围挖后,我得到这个,而不是工作:

[[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];

9
投票

我使用它。

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

添加一行(AppDelegate中)和所有UIAlertController作品。


6
投票

我有同样的任务,我做了这个技巧的今天,脏的,但它的工作原理

class CustomAlertViewController: UIAlertController {

    internal var cancelText: String?


    private var font: UIFont? = UIFont(name: "MuseoSansCyrl-500", size: 12)

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.tintColor = UIColor.blackColor()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        self.findLabel(self.view) //if you need ios 9 only, this can be made in viewWillAppear
    }

    func findLabel(scanView: UIView!) {
        if (scanView.subviews.count > 0) {
            for subview in scanView.subviews {
                if let label: UILabel = subview as? UILabel {

                    if (self.cancelText != nil && label.text == self.cancelText!) {
                        dispatch_async(dispatch_get_main_queue(),{
                            label.textColor = UIColor.redColor() //for ios 8.x
                            label.tintColor = UIColor.redColor() //for ios 9.x
                        })
                    }

                    if (self.font != nil) {
                        label.font = self.font
                    }
                }

                self.findLabel(subview)
            }
        }
    }

}

3
投票

更改tintColorwindow为我工作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

这样写:

[self.window setTintColor:[UIColor redColor]];

这一切都改变UIActionSheet对象的字体颜色。


3
投票

对于iOS 7的向后兼容性,并收集了正确的默认着色颜色(苹果很可能在未来的版本改变),我用这个。由于有关default tint及以上Lucas's answer答案。

    const Class alertControllerClass = NSClassFromString(@"UIAlertController");
    if(alertControllerClass)
    {
        UIColor *defaultTintColour = UIView.new.tintColor;
        [[UIView appearanceWhenContainedIn: alertControllerClass, nil] setTintColor: defaultTintColour];
    }

注意虽然 - 你需要确保你使用这个您开始设置全局色调,否则会UIView.new.tintColor只是给你设置了当前色调之前。


0
投票

斯威夫特4

    let alert =  UIAlertController(title: "title", message: "message", preferredStyle: .alert)
    alert.view.tintColor = UIColor.black
    self.present(alert, animated: true, completion: nil)

0
投票

对于斯威夫特,你可以这样做

    let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in

     }

    alertAction.setValue(UIColor.red, forKey: "titleTextColor")
© www.soinside.com 2019 - 2024. All rights reserved.