Is there any way to add UIPickerView into UIAlertController (Alert or ActionSheet) in Swift?

后端 未结 11 1345
旧巷少年郎
旧巷少年郎 2020-12-01 06:16

I\'m totally new to swift (and iOS programming at all), but I started messing around with it (it wasn\'t a good idea when everything is still beta version :D). So I tried to

11条回答
  •  不知归路
    2020-12-01 07:03

    Swift 5.2 / Xcode 11.6 / iOS 11.4

    func addAlert(){
    
        // create the alert
        let title = "This is the title"
        let message = "This is the message"
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert);
        alert.isModalInPopover = true;
    
        // add an action button
        let nextAction: UIAlertAction = UIAlertAction(title: "Action", style: .default){action->Void in
            // do something
        }
        alert.addAction(nextAction)
    
        // now create our custom view - we are using a container view which can contain other views
        let containerViewWidth = 250
        let containerViewHeight = 120
        let containerFrame = CGRect(x:10, y: 70, width: CGFloat(containerViewWidth), height: CGFloat(containerViewHeight));
        let containerView: UIView = UIView(frame: containerFrame);
    
        alert.view.addSubview(containerView)
    
        // now add some constraints to make sure that the alert resizes itself
        let cons:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1.00, constant: 130)
    
        alert.view.addConstraint(cons)
    
        let cons2:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1.00, constant: 20)
    
        alert.view.addConstraint(cons2)
    
        // present with our view controller
        present(alert, animated: true, completion: nil)
    
    }
    

提交回复
热议问题