Change Text Color of Items in UIActionSheet - iOS 8

前端 未结 9 1378
星月不相逢
星月不相逢 2020-11-29 02:26

I had been using following code to change text color of items which I add in UIActionSheet.:

- (void)willPresentActionSheet:(UIActionSheet *)act         


        
相关标签:
9条回答
  • 2020-11-29 03:03

    I have same task and I've made this hack today, dirty, but it works

    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)
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 03:04

    Swift 4

        let alert =  UIAlertController(title: "title", message: "message", preferredStyle: .alert)
        alert.view.tintColor = UIColor.black
        self.present(alert, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-11-29 03:09

    Ok, I ran into the same problem but I think I have found a solution:

    The appropriate way should be like this and I guess it works in iOS7:

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

    But it will not work in iOS8 due to the fact that the ActionSheets "buttons" is now based on a UICollectionView. So after some digging around I got this to work instead:

    [[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
    
    0 讨论(0)
提交回复
热议问题