UIAlertController(actionsheet)大小问题

时间:2018-02-09 07:57:09

标签: ios swift uialertcontroller

我面临如下问题,它发生在iOS 10.x

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alertController.popoverPresentationController?.sourceView = label
        alertController.popoverPresentationController?.sourceRect = label.bounds

        let action1 = UIAlertAction(title:"test", style: .default) { [weak self] _ in
        }

        let action2 = UIAlertAction(title:"test" style: .default) { [weak self] _ in
        }
 alertController.addAction(action1)
 alertController.addAction(action2)


        present(alertController, animated: true, completion: nil)

当我打开这个带有标签的警报控制器时,我看到我的alertcontroller是附加屏幕。它的按钮不可见。

enter image description here

2018-02-10 23:13:16.814325 Project[27392:1655456] [LayoutConstraints] 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:0x61800048af50 UIView:0x7fd79b6a87b0.width == - 20   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x61800048af50 UIView:0x7fd79b6a87b0.width == - 20   (active)>

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

UIWindow:0x7fd79b51c5f0 - AMBIGUOUS LAYOUT
|   UITransitionView:0x7fd79b665e70
|   UITransitionView:0x7fd79b4a2620
|   |   •UIView:0x7fd7a0c36970
|   |   |   *UIView:0x7fd7a0c367d0
|   |   |   |   •UIView:0x7fd79b4afbd0
|   |   |   |   |   *UIView:0x7fd79b4afd70
|   |   |   |   |   |   *UIView:0x7fd79b4aff10
|   |   |   |   |   |   |   *UIImageView:0x7fd79b4b00b0
|   |   |   |   |   |   |   *UILabel:0x7fd7a0c25dd0'Hello!'
|   |   |   |   |   *UILabel:0x7fd7a0c45680'Need help?'
|   |   |   |   |   *findCodeButton:0x7fd79b4af7e0
|   |   |   |   |   |   UIButtonLabel:0x7fd79b4aef50'tips for sett...'
|   |   |   |   |   *codeField:0x7fd79c16ba00
|   |   |   |   |   |   *UIView:0x7fd7a0c45910
|   |   |   |   |   |   |   *UILabel:0x7fd7a0c45c50'ENTER HERE'
|   |   |   |   |   |   *UIView:0x7fd7a0c45ab0
|   |   |   |   |   |   *_UIBaselineLayoutStrut:0x7fd7a0c47040
|   |   |   |   |   |   UITextFieldLabel:0x7fd7a0917750'Enter here'
|   |   |   |   |   *UIView:0x7fd7a0c47400
|   |   |   |   |   |   *nextButton:0x7fd79b4af520
|   |   |   |   |   |   |   UIButtonLabel:0x7fd79b422190'Next'
|   |   |   |   |   |   *UILabel:0x7fd7a0c475a0'Activate...'
|   |   |   |   |   |   *supportButton:0x7fd7a0c47b50
|   |   |   |   |   *UIButton:0x7fd7a0c480a0'Next'
|   |   |   |   |   |   UIButtonLabel:0x7fd79b4b4e30'Next'
|   |   |   |   |   *_UILayoutGuide:0x7fd7a0c48360
|   |   |   |   |   *_UILayoutGuide:0x7fd7a0c48510
|   |   |   |   |   *NoNetworkConnectionView:0x7fd79b69de20
|   |   |   |   |   |   *UIView:0x7fd79b69dff0
|   |   |   |   |   |   *UILabel:0x7fd79b69e190'No Internet'
|   |   |   |   |   |   *UIView:0x7fd79b69e420
|   |   |   |   |   |   |   *UIActivityIndicatorView:0x7fd79b69e5c0
|   |   |   |   |   |   |   |   UIImageView:0x7fd79b69e7f0
|   |   |   |   |   |   |   *UIImageView:0x7fd79b69e9d0
|   |   |   |   |   |   *UILabel:0x7fd79b69ed30'Make sure you...'
|   |   |   *ContainmentViewController...:0x7fd79b6930f0
|   |   |   |   UIImageView:0x7fd79b695c40
|   |   |   *_UILayoutGuide:0x7fd7a0c36b10
|   |   |   *_UILayoutGuide:0x7fd7a0c36cc0
|   +UITransitionView:0x7fd79b4c2370- AMBIGUOUS LAYOUT for UITransitionView:0x7fd79b4c2370.minX{id: 4329}, UITransitionView:0x7fd79b4c2370.minY{id: 4322}, UITransitionView:0x7fd79b4c2370.Width{id: 4473}, UITransitionView:0x7fd79b4c2370.Height{id: 4323}
|   |   UIView:0x7fd79b6a7bc0
|   |   *_UIKeyboardLayoutAlignmentView:0x7fd79b6a7230- AMBIGUOUS LAYOUT for _UIKeyboardLayoutAlignmentView:0x7fd79b6a7230.minX{id: 4328}, _UIKeyboardLayoutAlignmentView:0x7fd79b6a7230.minY{id: 4320}
|   |   *_UIAlertControllerView:0x7fd79b6a8090- AMBIGUOUS LAYOUT for _UIAlertControllerView:0x7fd79b6a8090.minX{id: 4474}, _UIAlertControllerView:0x7fd79b6a8090.minY{id: 4475}, _UIAlertControllerView:0x7fd79b6a8090.Width{id: 4476}, _UIAlertControllerView:0x7fd79b6a8090.Height{id: 4477}
|   |   |   *UIView:0x7fd79b6a87b0- AMBIGUOUS LAYOUT for UIView:0x7fd79b6a87b0.minX{id: 4456}, UIView:0x7fd79b6a87b0.minY{id: 4478}, UIView:0x7fd79b6a87b0.Height{id: 4452}
|   |   |   |   *_UIAlertControllerInterfaceActionGroupView:0x7fd79b6a8950- AMBIGUOUS LAYOUT for _UIAlertControllerInterfaceActionGroupView:0x7fd79b6a8950.minX{id: 4343}, _UIAlertControllerInterfaceActionGroupView:0x7fd79b6a8950.minY{id: 4350}
|   |   |   |   |   *UIView:0x7fd79b6a8d30- AMBIGUOUS LAYOUT for UIView:0x7fd79b6a8d30.minX{id: 4356}, UIView:0x7fd79b6a8d30.minY{id: 4361}
|   |   |   |   |   |   *_UIInterfaceActionGroupHeaderScrollView:0x7fd79ba50a00- AMBIGUOUS LAYOUT for _UIInterfaceActionGroupHeaderScrollView:0x7fd79ba50a00.minX{id: 4383}, _UIInterfaceActionGroupHeaderScrollView:0x7fd79ba50a00.minY{id: 4337}
|   |   |   |   |   |   |   *UIView:0x7fd79b6ae350- AMBIGUOUS LAYOUT for UIView:0x7fd79b6ae350.minX{id: 4479}, UIView:0x7fd79b6ae350.minY{id: 4331}, UIView:0x7fd79b6ae350.Width{id: 4465}
|   |   |   |   |   |   |   |   *UIView:0x7fd79b6ae880- AMBIGUOUS LAYOUT for UIView:0x7fd79b6ae880.minX{id: 4455}, UIView:0x7fd79b6ae880.minY{id: 4480}, UIView:0x7fd79b6ae880.Height{id: 4462}
|   |   |   |   |   |   |   |   *UIView:0x7fd79b6aea20- AMBIGUOUS LAYOUT for UIView:0x7fd79b6aea20.minX{id: 4469}, UIView:0x7fd79b6aea20.minY{id: 4481}, UIView:0x7fd79b6aea20.Width{id: 4464}
|   |   |   |   |   |   *groupView.actionsSequence...:0x7fd79b8d8600- AMBIGUOUS LAYOUT for groupView.actionsSequence....minX{id: 4387}, groupView.actionsSequence....minY{id: 4336}
|   |   |   |   |   |   |   actions-separatableSequen...:0x7fd79b6a9110
|   |   |   |   |   |   |   |   •actions-separatableSequen...:0x7fd79b6a92e0, MISSING HOST CONSTRAINTS
|   |   |   |   |   *_UIDimmingKnockoutBackdropView:0x7fd79b6a94a0- AMBIGUOUS LAYOUT for _UIDimmingKnockoutBackdropView:0x7fd79b6a94a0.minX{id: 4372}, _UIDimmingKnockoutBackdropView:0x7fd79b6a94a0.minY{id: 4377}
|   |   |   |   |   |   UIView:0x7fd79b6a9ef0
|   |   |   |   |   |   UIVisualEffectView:0x7fd79b6a9660
|   |   |   |   |   |   |   _UIVisualEffectBackdropView:0x7fd79b6a9b80
|   |   |   |   |   |   |   _UIVisualEffectFilterView:0x7fd79b6a9d50
|   |   |   |   *_UIAlertControllerInterfaceActionGroupView:0x7fd79b6abd50- AMBIGUOUS LAYOUT for _UIAlertControllerInterfaceActionGroupView:0x7fd79b6abd50.minX{id: 4399}, _UIAlertControllerInterfaceActionGroupView:0x7fd79b6abd50.minY{id: 4406}
|   |   |   |   |   *UIView:0x7fd79b6ac130- AMBIGUOUS LAYOUT for UIView:0x7fd79b6ac130.minX{id: 4412}, UIView:0x7fd79b6ac130.minY{id: 4417}
|   |   |   |   |   |   *groupView.actionsSequence...:0x7fd79ba4c400- AMBIGUOUS LAYOUT for groupView.actionsSequence....minX{id: 4439}, groupView.actionsSequence....minY{id: 4442}
|   |   |   |   |   |   |   actions-separatableSequen...:0x7fd79b6ac690
|   |   |   |   |   |   |   |   •actions-separatableSequen...:0x7fd79b6ac860, MISSING HOST CONSTRAINTS
|   |   |   |   |   *_UIDimmingKnockoutBackdropView:0x7fd79b6aca20- AMBIGUOUS LAYOUT for _UIDimmingKnockoutBackdropView:0x7fd79b6aca20.minX{id: 4428}, _UIDimmingKnockoutBackdropView:0x7fd79b6aca20.minY{id: 4433}
|   |   |   |   |   |   UIView:0x7fd79b6ad470
|   |   |   |   |   |   UIVisualEffectView:0x7fd79b6acbe0
|   |   |   |   |   |   |   _UIVisualEffectBackdropView:0x7fd79b6ad100
|   |   |   |   |   |   |   _UIVisualEffectFilterView:0x7fd79b6ad2d0

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host

更新:显示alertcontroller的视图作为childviewcontroller添加到容器上。不确定它是否会有所帮助。

5 个答案:

答案 0 :(得分:5)

您需要将两个操作添加到alertController。

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alertController.popoverPresentationController?.sourceView = self.myLabel
    alertController.popoverPresentationController?.sourceRect = self.myLabel!.frame

    let action1 = UIAlertAction(title:"Check", style: .default) { [weak self] _ in
        print("Check")
    }

    let action2 = UIAlertAction(title: "UnCheck", style: .default) { [weak self] _ in
        print("UnCheck")
    }
    alertController.addAction(action1)
    alertController.addAction(action2)
    self.present(alertController, animated: true, completion: nil)

enter image description here

答案 1 :(得分:1)

这肯定会对您有所帮助。您可以通过添加适当的约束来控制警报视图的高度/宽度。 (快速4.2)

let alert = UIAlertController(title: "Alert", message: "Some message", preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in

}))
alert.addAction(UIAlertAction(title: "Some", style: .default))
alert.addAction(UIAlertAction(title: "Some", style: .default))

let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: view.frame.height)
alert.view.addConstraint(height);
present(alert, animated: true)

答案 2 :(得分:0)

也许您应该向警报控制器添加操作。

alertController.addAction(action1)
alertController.addAction(action2)

答案 3 :(得分:0)

好像你已经创建了UIAlertAction项目,但没有将它们添加到UIAlertController。

在呈现UIAlertController之前尝试添加以下代码,

alertController.addAction(action1)
alertController.addAction(action2)

答案 4 :(得分:0)

添加此alertController.modalPresentationStyle = .popover

并尝试通过

提供preferredContentSize
alertController.preferredContentSize = yourPreferedSize