通过UIToolBar的UIBarButtonItem在iOS13上不显示

BB-*_*B-8 5 objective-c uitoolbar uibarbuttonitem ios ios13

我通过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];
Run Code Online (Sandbox Code Playgroud)

在此代码中,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.
Run Code Online (Sandbox Code Playgroud)

苹果开发人员论坛上有一个问题描述了同样的现象。

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

据此,这是一个限制问题,但是它是关于私有API的一部分,并且据信不可能进行进一步的研究和解决。

本文指出您使用UIStackView而不使用UIToolBar。

我已经向Apple报告了有关此问题的错误,但此时Apple没有任何响应,并且该问题尚未在新的Beta版iOS13(iOS13.1 beta)中得到解决。

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

我应该使用Apple Developer Forum文章中所述的UIStackView代替UIToolBar吗?

Chr*_*sen 6

从 iOS 13 开始,您需要给工具栏一个框架。知道问题是objective-c,但回复很快。

   doneToolbar.frame = CGRect(x: 0,
                              y: 0,
                              width: view.bounds.width,
                              height: 40)
Run Code Online (Sandbox Code Playgroud)

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