How to change the background color of the UIAlertController?

前端 未结 10 1446
耶瑟儿~
耶瑟儿~ 2020-11-30 03:49

Due to strange behavior of UIActionSheet in iOS 8, I have implemented UIAlertController with UIAction as buttons in it. I would like to change the entire background of the U

相关标签:
10条回答
  • 2020-11-30 04:34

    For Objective - C Code May be Like.

    UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
                                                                  message:@"Message"
                                                           preferredStyle:UIAlertControllerStyleAlert];
    UIView *firstSubview = alert.view.subviews.firstObject;
    UIView *alertContentView = firstSubview.subviews.firstObject;
    for (UIView *subSubView in alertContentView.subviews) {
        subSubView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
       //Close Action
    }];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-11-30 04:39

    Swift3

    Step into one more layer compare with swift2

        let subview1 = alert.view.subviews.first! as UIView
        let subview2 = subview1.subviews.first! as UIView
        let view = subview2.subviews.first! as UIView
    
        subview.backgroundColor = backgroundColor
        view.backgroundColor = backgroundColor
        view.layer.cornerRadius = 10.0
        
        // set color to UILabel font
        setSubviewLabelsToTextColor(textColor, view: view)
        
        // set font to alert via KVC, otherwise it'll get overwritten
        let titleAttributed = NSMutableAttributedString(
            string: alert.title!,
            attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 17)])
        alert.setValue(titleAttributed, forKey: "attributedTitle")
        
        let messageAttributed = NSMutableAttributedString(
            string: alert.message!,
            attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
        alert.setValue(messageAttributed, forKey: "attributedMessage")
    
        // set the buttons to non-blue, if we have buttons
        if let buttonColor = buttonColor {
            alert.view.tintColor = buttonColor
        }
    
    0 讨论(0)
  • 2020-11-30 04:40

    I have found a hack-ish way of doing it. First you need an extension to allow you to search for the UIVisualEffectView inside the UIAlertController:

    extension UIView
    {
        func searchVisualEffectsSubview() -> UIVisualEffectView?
        {
            if let visualEffectView = self as? UIVisualEffectView
            {
                return visualEffectView
            }
            else
            {
                for subview in subviews
                {
                    if let found = subview.searchVisualEffectsSubview()
                    {
                        return found
                    }
                }
            }
    
            return nil
        }
    }
    

    Important: You have to call this function after calling presentViewController, because only after loading the view controller that the visual effects view is inserted into place. Then you can change the effect associated with it to a dark blur effect:

    self.presentViewController(actionController, animated: true, completion: nil)
    
    if let visualEffectView = actionController.view.searchVisualEffectsSubview()
    {
        visualEffectView.effect = UIBlurEffect(style: .Dark)
    }
    

    And this is the final result:

    demo picture

    I am honestly surprised myself how well it works! I think this is probably something Apple forgot to add. Also, I haven't yet passed an App through approval with this "hack" (it isn't a hack because we're only using public APIs), but I'm confident there won't be a problem.

    0 讨论(0)
  • 2020-11-30 04:42

    for Swift 3/ Swift 4

    let subview =(alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
    
                subview.backgroundColor = UIColor(red: (145/255.0), green: (200/255.0), blue: (0/255.0), alpha: 1.0)
    
                alert.view.tintColor = UIColor.black
    

    .

    0 讨论(0)
提交回复
热议问题