警报弹出框的约束问题

时间:2020-07-29 20:02:00

标签: ios swift xcode

在这里快速输入一个。我尝试设置断点,但似乎看不出问题的根源。所以这是代码:

这是一个弹出警报按钮,在单击时会出现约束问题。

private lazy var SocialBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(" + ", for: .normal)
        
        button.translatesAutoresizingMaskIntoConstraints = false
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
        button.setTitleColor(.red, for: .normal)
        button.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
        return button
    }()

这是我设置按钮的方式:

 @objc private func btnPressed(_ sender: UIButton) {
        
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.modalPresentationStyle = .popover
        
        let image = UIImage(named: "instagram")
        let imageView = UIImageView()
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        imageView.frame =  CGRect(x: 25, y: 12, width: 35, height: 35)
        alert.view.addSubview(imageView)
        
        let image1 = UIImage(named: "tripadvisor")
        let imageView1 = UIImageView()
        imageView1.image = image1
        imageView1.contentMode = .scaleAspectFit
        alert.view.addSubview(imageView1)
        imageView1.frame = CGRect(x: 25, y: 60, width: 40, height: 50)
        
        let shareExternal = UIAlertAction(title: NSLocalizedString("Tripadvisor", comment: ""), style: .default) { action in
            
            if let url = URL(string: self.link1) {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:])
                }
            }
        }
        let shareInApp = UIAlertAction(title: "Instagram", style: .default)   {
            action in
            
            print("Insta")
            
            if let url = URL(string: self.link2) {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:])
                }
            }
        }
        
        let cancel = UIAlertAction(title: "Cancel", style: .cancel)   {
            action in
            
            print("cancel")
        }
        
        alert.addAction(shareInApp)
        alert.addAction(shareExternal)
        alert.addAction(cancel)
        
        if let presenter = alert.popoverPresentationController
        {
            presenter.sourceView = SocialBtn
            presenter.sourceRect = SocialBtn.bounds
        }
        
        present(alert, animated: true, completion: nil)
    }

当我单击按钮时,给我的问题是:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x283694e60 UIView:0x116024990.width == - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283694e60 UIView:0x116024990.width == - 16   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

请指向正确的方向。

0 个答案:

没有答案