UIAlertControllerInterfaceActionGroupView 不明确的布局

UIAlertControllerInterfaceActionGroupView AMBIGUOUS LAYOUT

我在展示我的 UIAlertController 时试图找出约束问题。

2019-04-26 11:40:42.140007+0100 MyAPP[12811:3974220] [LayoutConstraints] 无法同时满足约束条件。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 ( “” ) 将尝试通过打破约束来恢复 在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点以在调试器中捕获它。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。

我使用以下函数创建并呈现 UIAlertController

@discardableResult
static func showOptionsModal(withOptions options:[String], sender:UIViewController ,holderView:UIView, arrowPosition:UIPopoverArrowDirection, completion: ((_ option:Int) -> ())?)->UIAlertController {

    let moreActionsCellSheetController = UIAlertController(title:nil, message: nil,preferredStyle: .actionSheet)

    for (i, option) in options.enumerated(){

        let optionAction = UIAlertAction(title:option, style: .default){ (_) in
            completion?(i)
        }
        moreActionsCellSheetController.addAction(optionAction)

    }

    if UIDevice.current.userInterfaceIdiom != .pad {
        let actionCancel = UIAlertAction(title:printLocalized(withKey: "messages.cancel", targetSpecific: false), style: .destructive){ (_) in
            completion?(-1)
        }
        moreActionsCellSheetController.addAction(actionCancel)
    }


    moreActionsCellSheetController.view.tintColor = UIColor.black

    moreActionsCellSheetController.popoverPresentationController?.sourceView = holderView
    moreActionsCellSheetController.popoverPresentationController?.sourceRect = holderView.frame

    switch arrowPosition {
    case .up:
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.y = 0
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.x = (-holderView.frame.width / 8)
        moreActionsCellSheetController.popoverPresentationController?.permittedArrowDirections = .up
    case .right:
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.y = 0
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.x = 0
        moreActionsCellSheetController.popoverPresentationController?.permittedArrowDirections = .right
    case .down:
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.y = holderView.bounds.width/2
        moreActionsCellSheetController.popoverPresentationController?.permittedArrowDirections = .down
    case .left:
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.y = holderView.bounds.height/2
        moreActionsCellSheetController.popoverPresentationController?.permittedArrowDirections = .left
    default:
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.y = 0
        moreActionsCellSheetController.popoverPresentationController?.sourceRect.origin.x = 0
        moreActionsCellSheetController.popoverPresentationController?.permittedArrowDirections = .any
    }

    sender.present(moreActionsCellSheetController,animated: true, completion: nil)

    return moreActionsCellSheetController

}

我添加了一个符号断点并添加了如下操作:

autolayoutTrace 显示以下结果:

•UIWindow:0x159d197b0 - AMBIGUOUS LAYOUT
|   •UIView:0x159d3fbf0
|   |   *<UILayoutGuide: 0x2817eda40 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 44}, {375, 734}}, owningView = <UIView: 0x159d3fbf0; frame = (0 0; 375 812); autoresize = W+H; tintColor = UIExtendedSRGBColorSpace 0.176471 0.176471 0.176471 1; layer = <CALayer: 0x282ec41a0>>>
|   |   *UIView:0x159d367d0
|   |   |   UILayoutContainerView:0x159d580b0
|   |   |   |   UINavigationTransitionView:0x159d5a760
|   |   |   |   |   UIViewControllerWrapperView:0x159d4e880
|   |   |   |   |   |   •UIView:0x15b403b70
|   |   |   |   |   |   |   *<UILayoutGuide: 0x2817fd5e0 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 0}, {375, 640}}, owningView = <UIView: 0x15b403b70; frame = (0 44; 375 640); autoresize = W+H; tintColor = UIExtendedSRGBColorSpace 0.176471 0.176471 0.176471 1; layer = <CALayer: 0x282e8ad20>>>
|   |   |   |   |   |   |   *DAT_Air_Vinyl.MainLabel:0x15b503030'Select device to connect ...'
|   |   |   |   |   |   |   *UIScrollView:0x15c014c00
|   |   |   |   |   |   |   |   *<_UIScrollViewContentOffsetGuide: 0x2817ecfc0 - "UIScrollView-contentOffsetLayoutGuide", layoutFrame = {{0, 0}, {0, 0}}, owningView = <UIScrollView: 0x15c014c00; frame = (0 0; 375 640); clipsToBounds = YES; hidden = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x282079e00>; layer = <CALayer: 0x282e8ce00>; contentOffset: {0, 0}; contentSize: {375, 746}; adjustedContentInset: {0, 0, 0, 0}>>
|   |   |   |   |   |   |   |   *DAT_Air_Vinyl.RecordHeaderView:0x15b503320
|   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.RecordHeaderView:0x15b406010
|   |   |   |   |   |   |   |   |   |   *<UILayoutGuide: 0x2817e5a40 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 0}, {375, 150}}, owningView = <DAT_Air_Vinyl.RecordHeaderView: 0x15b406010; frame = (0 0; 375 150); clipsToBounds = YES; autoresize = RM+BM; layer = <CALayer: 0x282e8b120>>>
|   |   |   |   |   |   |   |   |   |   *UIView:0x15b406240
|   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainButton:0x15b406420'Enable'
|   |   |   |   |   |   |   |   |   |   |   |   UIImageView:0x159d74e80
|   |   |   |   |   |   |   |   |   |   |   |   UIButtonLabel:0x15b406950'Enable'
|   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainView:0x15b4070b0
|   |   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainLabel:0x15b4075f0'Gain:'
|   |   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.SelectorDropDownView:0x15b407ae0
|   |   |   |   |   |   |   |   |   |   |   |   |   *UIImageView:0x15b604f90
|   |   |   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainLabel:0x15b60aa20'0 dB'
|   |   |   |   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainButton:0x15b4041a0'Clear all'
|   |   |   |   |   |   |   |   |   |   |   |   UIImageView:0x159d79740
|   |   |   |   |   |   |   |   |   |   |   |   UIButtonLabel:0x15b4046d0'Clear all'
|   |   |   |   |   |   |   |   |   |   *UIView:0x15b508ed0
|   |   |   |   |   |   |   |   |   *UIView:0x15b50d250
|   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainTableView:0x15c025c00
|   |   |   |   |   |   |   |   |   UIView:0x15b5084d0
|   |   |   |   |   |   |   |   |   UIImageView:0x159d767c0
|   |   |   |   |   |   |   |   |   UIImageView:0x159d769f0
|   |   |   |   |   |   |   |   UIImageView:0x159d76f70
|   |   |   |   |   |   |   |   UIImageView:0x159d76d40
|   |   |   |   |   |   |   *_TtCC13DAT_Air_Vinyl20SelectorDropDownView12DropDownView:0x15b509420
|   |   |   |   |   |   |   |   *DAT_Air_Vinyl.MainTableView:0x15c026800
|   |   |   |   |   |   |   |   |   UIView:0x15b50a210
|   |   |   |   |   |   |   |   |   UIImageView:0x159d7a740
|   |   |   |   UINavigationBar:0x159d582b0
|   |   |   |   |   _UIBarBackground:0x159d58770
|   |   |   |   |   |   UIImageView:0x159d58c00
|   |   |   |   |   _UINavigationBarLargeTitleView:0x159d59880
|   |   |   |   |   |   UILabel:0x159d59dc0
|   |   |   |   |   •_UINavigationBarContentView:0x159d59260
|   |   |   |   |   |   *<UILayoutGuide: 0x2817ee060 - "BackButtonGuide(0x159d59720)", layoutFrame = {{0, 0}, {8, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *<UILayoutGuide: 0x2817ee140 - "LeadingBarGuide(0x159d59720)", layoutFrame = {{8, 0}, {0, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *<UILayoutGuide: 0x2817ee220 - "TitleView(0x159d59720)", layoutFrame = {{8, 0}, {359, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *<UILayoutGuide: 0x2817ee300 - "TrailingBarGuide(0x159d59720)", layoutFrame = {{367, 0}, {0, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *<UILayoutGuide: 0x2817ee3e0 - "UIViewLayoutMarginsGuide", layoutFrame = {{16, 0}, {343, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *<UILayoutGuide: 0x2817e5ce0 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 0}, {375, 44}}, owningView = <_UINavigationBarContentView: 0x159d59260; frame = (0 0; 375 44); layer = <CALayer: 0x282eaea60>>>
|   |   |   |   |   |   *_UITAMICAdaptorView:0x15b50e7a0
|   |   |   |   |   |   |   DAT_Air_Vinyl.RecordViewNavigationBar:0x15b504370
|   |   |   |   |   |   |   |   UIImageView:0x15b50d430
|   |   |   |   |   |   |   |   DAT_Air_Vinyl.ScrollableTextView:0x15b50d660
|   |   |   |   |   |   |   |   |   UIScrollView:0x15c017200
|   |   |   |   |   |   |   |   |   |   DAT_Air_Vinyl.MainLabel:0x15b50d8c0'DAT-Air WSS (95B6DC)'
|   |   |   |   |   |   |   |   |   |   DAT_Air_Vinyl.MainLabel:0x15b50dbb0'DAT-Air WSS (95B6DC)'
|   |   |   |   |   |   |   |   UIButton:0x15b50dea0
|   |   |   |   |   |   |   |   |   UIImageView:0x159d773f0
|   |   |   |   |   _UINavigationBarModernPromptView:0x159d5a0b0
|   |   *UIView:0x159d369b0
|   |   |   *DAT_Air_Vinyl.MiniPlayerView:0x159d458c0
|   |   |   |   *<UILayoutGuide: 0x2817edb20 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 0}, {375, 1}}, owningView = <DAT_Air_Vinyl.MiniPlayerView: 0x159d458c0; frame = (0 0; 375 1); autoresize = W+H; gestureRecognizers = <NSArray: 0x2820829a0>; layer = <CALayer: 0x282eda960>>>
|   |   |   |   *UIImageView:0x159e0ec60
|   |   |   |   *DAT_Air_Vinyl.ScrollableTextView:0x159e10b20
|   |   |   |   |   UIScrollView:0x15a828200
|   |   |   |   |   |   DAT_Air_Vinyl.MainLabel:0x159e10f80
|   |   |   |   |   |   DAT_Air_Vinyl.MainLabel:0x159d482b0
|   |   |   |   *UIButton:0x159d44cf0
|   |   |   |   |   UIImageView:0x159d3ea10
|   |   |   |   *UIView:0x159d489a0
|   |   |   |   *UIView:0x159d48b80
|   |   *UITabBar:0x159d36b90
|   |   |   _UIBarBackground:0x159d3ec60
|   |   |   |   UIImageView:0x159d3f390
|   |   |   |   UIVisualEffectView:0x159d3f5c0
|   |   |   |   |   _UIVisualEffectBackdropView:0x159d47010
|   |   |   |   |   _UIVisualEffectSubview:0x159d3c420
|   |   |   |   |   _UIVisualEffectSubview:0x159d3da80
|   |   |   UITabBarButton:0x159d404b0
|   |   |   |   UITabBarSwappableImageView:0x159d33330
|   |   |   |   UITabBarButtonLabel:0x159d407d0'Library'
|   |   |   UITabBarButton:0x159d414f0
|   |   |   |   UITabBarSwappableImageView:0x159d41d10
|   |   |   |   UITabBarButtonLabel:0x159d419f0'Record'
|   |   |   UITabBarButton:0x159d42840
|   |   |   |   UITabBarSwappableImageView:0x159d43060
|   |   |   |   UITabBarButtonLabel:0x159d42d40'Exports'
|   |   |   UITabBarButton:0x159d43b90
|   |   |   |   UITabBarSwappableImageView:0x159d443b0
|   |   |   |   UITabBarButtonLabel:0x159d44090'Settings'
|   +UITransitionView:0x15b1084e0- AMBIGUOUS LAYOUT for UITransitionView:0x15b1084e0.minX{id: 1503}, UITransitionView:0x15b1084e0.minY{id: 1480}, UITransitionView:0x15b1084e0.Width{id: 1608}, UITransitionView:0x15b1084e0.Height{id: 1481}
|   |   UIView:0x15b519280
|   |   *_UIKeyboardLayoutAlignmentView:0x15b518e50- AMBIGUOUS LAYOUT for _UIKeyboardLayoutAlignmentView:0x15b518e50.minY{id: 1478}
|   |   *_UIAlertControllerView:0x15d815e00- AMBIGUOUS LAYOUT for _UIAlertControllerView:0x15d815e00.minX{id: 1609}, _UIAlertControllerView:0x15d815e00.minY{id: 1610}, _UIAlertControllerView:0x15d815e00.Width{id: 1611}, _UIAlertControllerView:0x15d815e00.Height{id: 1612}
|   |   |   *UIView:0x15b103200- AMBIGUOUS LAYOUT for UIView:0x15b103200.minX{id: 1582}, UIView:0x15b103200.minY{id: 1601}
|   |   |   |   *_UIAlertControllerInterfaceActionGroupView:0x15b510760- AMBIGUOUS LAYOUT for _UIAlertControllerInterfaceActionGroupView:0x15b510760.minX{id: 1522}
|   |   |   |   |   *<_UIContentConstraintsLayoutGuide: 0x15b510c40 - "", layoutFrame = {{0, 0}, {39, 171.66666666666666}}, owningView = <_UIAlertControllerInterfaceActionGroupView: 0x15b510760; frame = (0 0; 0 0); opaque = NO; gestureRecognizers = <NSArray: 0x282061bf0>; layer = <CALayer: 0x282e8dde0>>>
|   |   |   |   |   *UIView:0x15b510fb0
|   |   |   |   |   |   *_UIInterfaceActionGroupHeaderScrollView:0x15c035000
|   |   |   |   |   |   |   *<_UIScrollViewContentOffsetGuide: 0x2817e6140 - "UIScrollView-contentOffsetLayoutGuide", layoutFrame = {{0, 0}, {0, 0}}, owningView = <_UIInterfaceActionGroupHeaderScrollView: 0x15c035000; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x282060ab0>; layer = <CALayer: 0x282ea68c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}>>
|   |   |   |   |   |   |   *UIView:0x15b515c10
|   |   |   |   |   |   |   |   *UIView:0x15b515df0- AMBIGUOUS LAYOUT for UIView:0x15b515df0.minX{id: 1580}, UIView:0x15b515df0.minY{id: 1812}, UIView:0x15b515df0.Height{id: 1813}
|   |   |   |   |   |   |   |   *UIView:0x15b515fd0- AMBIGUOUS LAYOUT for UIView:0x15b515fd0.minX{id: 1589}, UIView:0x15b515fd0.minY{id: 1814}, UIView:0x15b515fd0.Width{id: 1588}, UIView:0x15b515fd0.Height{id: 1815}
|   |   |   |   |   |   |   |   *UIView:0x15b5161b0- AMBIGUOUS LAYOUT for UIView:0x15b5161b0.minX{id: 1599}, UIView:0x15b5161b0.minY{id: 1816}
|   |   |   |   |   |   *groupView.actionsSequence...:0x15c032c00
|   |   |   |   |   |   |   +actions-separatableSequen...:0x15b511550
|   |   |   |   |   |   |   |   •actions-separatableSequen...:0x15b511970
|   |   |   |   |   |   |   |   |   *_UIInterfaceActionCustomViewRepresentationView:0x15b10cc30- AMBIGUOUS LAYOUT for _UIInterfaceActionCustomViewRepresentationView:0x15b10cc30.Height{id: 1650}
|   |   |   |   |   |   |   |   |   |   +_UIAlertControllerActionView:0x15b516e90
|   |   |   |   |   |   |   |   |   |   |   *UIView:0x15b517370
|   |   |   |   |   |   |   |   |   |   |   |   *UILabel:0x15b517550'Show devices'
|   |   |   |   |   |   |   |   |   *_UIInterfaceActionItemSeparatorView_iOS:0x15b60c530- AMBIGUOUS LAYOUT for _UIInterfaceActionItemSeparatorView_iOS:0x15b60c530.minY{id: 1725}
|   |   |   |   |   |   |   |   |   |   UIView:0x15b60c930
|   |   |   |   |   |   |   |   |   |   UIView:0x15b60cb10
|   |   |   |   |   |   |   |   |   *_UIInterfaceActionCustomViewRepresentationView:0x15b60bda0- AMBIGUOUS LAYOUT for _UIInterfaceActionCustomViewRepresentationView:0x15b60bda0.minY{id: 1727}, _UIInterfaceActionCustomViewRepresentationView:0x15b60bda0.Height{id: 1700}
|   |   |   |   |   |   |   |   |   |   +_UIAlertControllerActionView:0x15b517840
|   |   |   |   |   |   |   |   |   |   |   *UIView:0x15b517b20
|   |   |   |   |   |   |   |   |   |   |   |   *UILabel:0x15b517d00'Add device'
|   |   |   |   |   |   |   |   |   *_UIInterfaceActionItemSeparatorView_iOS:0x15b60d3c0- AMBIGUOUS LAYOUT for _UIInterfaceActionItemSeparatorView_iOS:0x15b60d3c0.minY{id: 1729}
|   |   |   |   |   |   |   |   |   |   UIView:0x15b60d5c0
|   |   |   |   |   |   |   |   |   |   UIView:0x15b60d7a0
|   |   |   |   |   |   |   |   |   *_UIInterfaceActionCustomViewRepresentationView:0x15b60bfe0- AMBIGUOUS LAYOUT for _UIInterfaceActionCustomViewRepresentationView:0x15b60bfe0.minY{id: 1723}, _UIInterfaceActionCustomViewRepresentationView:0x15b60bfe0.Height{id: 1653}
|   |   |   |   |   |   |   |   |   |   +_UIAlertControllerActionView:0x15b517ff0
|   |   |   |   |   |   |   |   |   |   |   *UIView:0x15b5182d0
|   |   |   |   |   |   |   |   |   |   |   |   *UILabel:0x15b5184b0'Cancel'
|   |   |   |   |   *_UIDimmingKnockoutBackdropView:0x15b512570
|   |   |   |   |   |   UIView:0x15b513340
|   |   |   |   |   |   UIVisualEffectView:0x15b512970
|   |   |   |   |   |   |   _UIVisualEffectBackdropView:0x15b512f30
|   |   |   |   |   |   |   _UIVisualEffectSubview:0x15b513140

约束问题似乎只发生在 iPhoneX 上。 Ipad 和 iPhone 7 和 7 plus 没有显示问题。

谢谢。

我在 iPad 遇到了类似的问题。

我修复了它,删除了 sourceRect 并将 view.frame 更改为 CGRect(x, y, 0, 0)。

那么,删除代码中的 moreActionsCellSheetController.popoverPresentationController?.sourceRect 怎么样?

sourceRect 表示UIAlertController 的位置。所以我觉得你最好把它设置成CGRect。

谢谢。