作者:倩女碧海蓝天_979 | 来源:互联网 | 2023-01-12 17:11
我正在测试一个简单的UIToolbar
,有两个UIBarButtonItem
作为项目,一个使用initWithCustomView:
方法构建,另一个使用initWithTitle:style:target:action:
方法.
第二个按钮可以在工具栏上查看,但第一个按钮丢失了.
问题仅发生在iOS 9和iOS 10上.在iOS 11上不会发生.
有谁有想法吗?
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setTranslatesAutoresizingMaskIntoConstraints:NO];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"one" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:audioOnIcon] forState:UIControlStateNormal];
[toolbar setItems:[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithCustomView:button],
[[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStylePlain target:self action:nil],
nil]];
[toolbar setBackgroundImage:[UIImage new] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[toolbar setShadowImage:[UIImage new] forToolbarPosition:UIBarPositionAny];
toolbar.accessibilityIdentifier = toolbarIdentifier;
[self.view insertSubview:toolbar atIndex:[self getSubviewIndex:toolbar.accessibilityIdentifier]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[subview]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{ @"subview": toolbar}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subview]-padding-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{ @"padding": @(2 * PADDING) } views:@{ @"subview": toolbar }]];
1> S1LENT WARRI..:
在将其添加到工具栏之前,您需要调用该sizeToFit
方法UIButton
.
像这样:
[button sizeToFit]; // (Objective-C)
OR
button.sizeToFit() // (Swift)
虽然它迟到了回答这个问题,但希望这可以帮助你或其他人.