【问题标题】:UIBarButtonItem via UIToolBar is not displayed on iOS13通过 UIToolBar 的 UIBarButtonItem 在 iOS13 上不显示
【发布时间】:2019-08-29 13:19:16
【问题描述】:

我通过 UIToolBar 将 UIBarButtonItem 设置为 iOS13 上 navigationItem 的 titleView。

构建并运行后,UIBarButtonItem 没有按预期显示。

// note
UIBarButtonItem *noteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCompose target:self action:@selector(noteViewAction)];

// camera
UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCamera target:self action:@selector(cameraViewAction)];

// spacer
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 50.0;


// Toolbar
UIToolbar *toolbar;
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 44.0f)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
toolbar.items = [NSArray arrayWithObjects:space, noteButton, space, cameraButton, nil];
toolbar.backgroundColor = [UIColor clearColor];

[self.navigationItem setTitleView:toolbar];

此代码中,UIBarButtonItem在iOS12.4.1及更早版本正确显示。

Xcode 控制台日志中显示以下消息。

2019-08-29 19:22:39.208598+0900 UINavigationItemBugOnIos13[559:49829] [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. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x2839cf980 h=--& v=--& _UIToolbarContentView:0x102c14c20.width == 0   (active)>",
    "<NSLayoutConstraint:0x2839cff20 UIImageView:0x102d2aff0.width <= _UIModernBarButton:0x102d29c40.width   (active)>",
    "<NSLayoutConstraint:0x2839c00f0 UIImageView:0x102d2ca80.width <= _UIModernBarButton:0x102d2b550.width   (active)>",
    "<NSLayoutConstraint:0x2839c9720 H:|-(0)-[_UIButtonBarStackView:0x102c18130]   (active, names: '|':_UIToolbarContentView:0x102c14c20 )>",
    "<NSLayoutConstraint:0x2839c9770 _UIButtonBarStackView:0x102c18130.trailing == _UIToolbarContentView:0x102c14c20.trailing   (active)>",
    "<NSLayoutConstraint:0x2839cd9f0 'IB_Leading_Leading' H:|-(16)-[_UIModernBarButton:0x102d29c40]   (active, names: '|':_UIButtonBarButton:0x102d29a60 )>",
    "<NSLayoutConstraint:0x2839ce490 'IB_Leading_Leading' H:|-(>=10)-[_UIModernBarButton:0x102d2b550]   (active, names: '|':_UIButtonBarButton:0x102d2b370 )>",
    "<NSLayoutConstraint:0x2839cda40 'IB_Trailing_Trailing' H:[_UIModernBarButton:0x102d29c40]-(>=11.5)-|   (active, names: '|':_UIButtonBarButton:0x102d29a60 )>",
    "<NSLayoutConstraint:0x2839ce4e0 'IB_Trailing_Trailing' H:[_UIModernBarButton:0x102d2b550]-(16)-|   (active, names: '|':_UIButtonBarButton:0x102d2b370 )>",
    "<NSLayoutConstraint:0x2839c4140 'UISV-canvas-connection' UILayoutGuide:0x2823ecfc0'UIViewLayoutMarginsGuide'.leading == UIView:0x102c3eb90.leading   (active)>",
    "<NSLayoutConstraint:0x2839c42d0 'UISV-canvas-connection' UILayoutGuide:0x2823ecfc0'UIViewLayoutMarginsGuide'.trailing == UIView:0x102c3ee70.trailing   (active)>",
    "<NSLayoutConstraint:0x2839c4320 'UISV-spacing' H:[UIView:0x102c3eb90]-(0)-[_UIButtonBarButton:0x102d29a60]   (active)>",
    "<NSLayoutConstraint:0x2839c4370 'UISV-spacing' H:[_UIButtonBarButton:0x102d29a60]-(0)-[UIView:0x102c3ed00]   (active)>",
    "<NSLayoutConstraint:0x2839c43c0 'UISV-spacing' H:[UIView:0x102c3ed00]-(0)-[_UIButtonBarButton:0x102d2b370]   (active)>",
    "<NSLayoutConstraint:0x2839cf070 'UISV-spacing' H:[_UIButtonBarButton:0x102d2b370]-(0)-[UIView:0x102c3ee70]   (active)>",
    "<NSLayoutConstraint:0x2839c9590 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x2823ecfc0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x102c18130 )>",
    "<NSLayoutConstraint:0x2839c9630 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x2823ecfc0'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x102c18130 )>"
)

Will attempt to recover by breaking constraint 
NSLayoutConstraint:0x2839c00f0 UIImageView:0x102d2ca80.width <= _UIModernBarButton:0x102d2b550.width   (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.

Apple 开发者论坛上有一个问题描述了相同的现象。

https://forums.developer.apple.com/thread/121474

据此,是限制的问题,但是是关于私有API的部分,写着无法进一步排查解决。

这篇文章说你使用了 UIStackView 而没有使用 UIToolBar。

我已经就这个问题向Apple报告了一个Bug,但Apple目前没有回应,并且在iOS13的新测试版(iOS13.1 beta)中也没有解决这个问题。

在 iOS13 中,通过 UIToolBar 的 UIBarButtonItem 是否不可用?

我应该使用 UIStackView 代替 Apple 开发者论坛文章中描述的 UIToolBar 吗?

【问题讨论】:

  • 补充。在这段代码中,UIBarButtonItem在代码执行时显示。但是,当显示另一个屏幕并显示原始屏幕时,重新绘制屏幕时,UIBarButtonItem会消失,或者显示UIAlertController。
  • 我遇到了同样的问题。有点怪!当我将 UIBarButtonItems 放入其中时,没有可见的 UIToolBar。 iOS 13.1 beta 和 Xcode 11 beta 7
  • 你们找到解决方案了吗?
  • 作为临时措施,我们决定不使用 viewDidLoad 进行处理,而是使用 viewWillAppear。此外,还进行了更改,以便在每次显示 UIAlertView 或更改屏幕绘图时执行按钮编辑和显示处理。我们已经向 Apple 提交了错误反馈,但目前还没有根本性的解决方案。

标签: ios objective-c uibarbuttonitem uitoolbar ios13


【解决方案1】:

从 iOS 13 开始,您需要为工具栏提供一个框架。知道问题是客观 c,但要迅速回复。

   doneToolbar.frame = CGRect(x: 0,
                              y: 0,
                              width: view.bounds.width,
                              height: 40)

另一种解决方案是将工具栏放在stackview 内

【讨论】:

  • 在上面的Objective-c代码中,UIToolBar在初始化的时候,Frame size是由initWithFrame定义的。另外,作为测试,我在使用initWithFrame初始化后立即添加了重新定义toolbar.frame的代码,但结果是一样的。 UIBarButtonItem 不能放在 UIStackView 中,所以很遗憾没有满足这个要求。 forums.developer.apple.com/message/379749#379749
  • @BB-8 是的,你是对的,我错过了在你的代码中设置的框架。不知道您无法将 UIToolbars 添加到 UIStackViews。我遇到了这个问题,并且能够通过设置框架来解决它,对不起,我无法提供更多帮助。
【解决方案2】:

它会阻止子视图调整大小。

toolbar.autoresizesSubviews = false;

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多