change UIAlertcontroller background Color

前端 未结 7 926
孤城傲影
孤城傲影 2021-02-02 15:09

Ok so I have this alert that I am using and I want the background of it to be black not grey like it is. I have managed to change the colour of the text for the title and the me

7条回答
  •  旧时难觅i
    2021-02-02 15:24

    try this

    Swift2 and below

    let subview :UIView = alert.view.subviews. first! as UIView
    let alertContentView = subview.subviews. first! as UIView
    alertContentView.backgroundColor = UIColor.blackColor()
    

    Objective -C

    UIView *subView = alertController.view.subviews.firstObject; //firstObject
    UIView *alertContentView = subView.subviews.firstObject; //firstObject
    [alertContentView setBackgroundColor:[UIColor darkGrayColor]];
    
    alertContentView.layer.cornerRadius = 5;
    

    updated answer swift 3 and above

       let alert = UIAlertController(title: "validate",message: "Check the process", preferredStyle: .alert)
        let dismissAction = UIAlertAction(title: "Dismiss", style: .destructive, handler: nil)
        alert.addAction(dismissAction)
        self.present(alert, animated: true, completion:  nil)
        // change the background color
        let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
        subview.layer.cornerRadius = 1
        subview.backgroundColor = UIColor(red: (195/255.0), green: (68/255.0), blue: (122/255.0), alpha: 1.0)
    

    output

    iPhone

    iPad

提交回复
热议问题