Swift default AlertViewController breaking constraints

前端 未结 9 683
野趣味
野趣味 2020-11-27 14:25

I am trying to use a default AlertViewController with style .actionSheet. For some reason, the alert causes a constraint error. As long as

9条回答
  •  遥遥无期
    2020-11-27 15:09

    Interesting ideas here. Personally I don't like the idea of deleting the constraint or changing it's value (size).

    As the issue hinges on the constraint resolution being forced into a position where it must break a mandated (priority 1000) constraint, a less brutal approach is just to tell the framework that this constraint could be broken if needed.

    So (based on Josh's "Safe" class):

    class PXAlertController: UIAlertController {
        override func viewDidLoad() {
            super.viewDidLoad()
            tweakProblemWidthConstraints()
        }
        
        func tweakProblemWidthConstraints() {
            for subView in self.view.subviews {
                for constraint in subView.constraints {
                    // Identify the problem constraint
                    // Check that it's priority 1000 - which is the cause of the conflict.
                    if constraint.firstAttribute == .width &&
                        constraint.constant == -16 &&
                        constraint.priority.rawValue == 1000 {
                        // Let the framework know it's okay to break this constraint
                        constraint.priority = UILayoutPriority(rawValue: 999)
                    }
                }
            }
        }
    }
    

    This has the advantages that it doesn't change any layout dimensions, it also stands a good chance of being well behaved in the event of a fix in the framework.

    Tested in iPhone SE simulator (which was giving me my original problem) - constraint related debug has gone.

提交回复
热议问题