How to change UIAlertController button text colour in iOS9?

后端 未结 14 1369
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 16:16

The question is similar to iOS 8 UIActivityViewController and UIAlertController button text color uses window's tintColor but in iOS 9.

I have a UIAlertControlle

14条回答
  •  星月不相逢
    2020-12-15 16:59

    Objective-C

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title text"  message:@"Message text"  preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    //code here…
    }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Later" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    //code here….
    }];
    [ok setValue:[UIColor greenColor] forKey:@"titleTextColor"];
    [cancel setValue:[UIColor redColor] forKey:@"titleTextColor"];
    [alertController addAction:ok];
    [alertController addAction:cancel];
    [alertController.view setTintColor:[UIColor yellowColor]];
    [self presentViewController:alertController animated:YES completion:nil];
    

    Swift 3

    let alertController = UIAlertController(title: "Title text", message: "Message text", preferredStyle: .alert)
    let ok = UIAlertAction(title: "Yes" , style: .default) { (_ action) in
                 //code here…
            }
    let cancel = UIAlertAction(title: "Later" , style: .default) { (_ action) in
                //code here…
            }
    ok.setValue(UIColor.green, forKey: "titleTextColor")
    cancel.setValue(UIColor.red, forKey: "titleTextColor")
    alertController.addAction(ok)
    alertController.addAction(cancel)
    alertController.view.tintColor = .yellow
    self.present(alertController, animated: true, completion: nil)
    

提交回复
热议问题