为什么我用于 UIBarButtonItem 的 UIButton 的高度没有变化?
Why isn't the UIButton I'm using for a UIBarButtonItem changing in height?
我正在创建一个按钮并将其作为 UIBarButtonItem
添加到下面的代码中。我正在努力使它成为一个完美的圆圈。
UIButton *hasMessageBtn = [UIButton alloc];
hasMessageBtn.backgroundColor = [UIColor orangeColor];
hasMessageBtn.layer.frame = CGRectMake(0, 0, 24, 24);
hasMessageBtn.layer.cornerRadius = 12;
[hasMessageBtn addTarget:self action:@selector(openMessages:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn];
self.navigationItem.leftBarButtonItem = messages;
不过,看起来尺寸在两个方面是错误的。高度永远不会改变,甚至当我改变宽度值时甚至认为宽度会改变,这似乎是不正确的,因为设置了圆角半径,
hasMessageBtn.layer.cornerRadius = 12;
没有完全封闭顶部和底部的圆角。我在这里错过了什么?
When you takes smaller the default size of custom view, its height will stretch the height to 34
. That's why your leftBarButton
getting distorted.
解法:
将高度和宽度约束应用于 barButton
(建议 *)
UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn];
[NSLayoutConstraint activateConstraints:@[[messages.customView.widthAnchor constraintEqualToConstant:24.0], [messages.customView.widthAnchor constraintEqualToConstant:24.0]]];
设置自定义视图的按钮大小,CGSize(34, 34)
hasMessageBtn.layer.frame = CGRectMake(0, 0, 34, 34);
hasMessageBtn.layer.cornerRadius = hasMessageBtn.frame.size.height / 2;
自 iOS11 起,您必须对栏按钮项设置约束。这是它在 objective-c:
中的样子
[hasMessageBtn.widthAnchor constraintEqualToConstant:24].active = YES;
[hasMessageBtn.heightAnchor constraintEqualToConstant:24].active = YES;
在swift中:
hasMessageBtn.heightAnchor.constraint(equalToConstant: 24).isActive = true
hasMessageBtn.widthAnchor.constraint(equalToConstant: 24).isActive = true
如果您对此主题有更多疑问,建议您查看 2017 WWDC 视频 Updating Your App for iOS 11
我正在创建一个按钮并将其作为 UIBarButtonItem
添加到下面的代码中。我正在努力使它成为一个完美的圆圈。
UIButton *hasMessageBtn = [UIButton alloc];
hasMessageBtn.backgroundColor = [UIColor orangeColor];
hasMessageBtn.layer.frame = CGRectMake(0, 0, 24, 24);
hasMessageBtn.layer.cornerRadius = 12;
[hasMessageBtn addTarget:self action:@selector(openMessages:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn];
self.navigationItem.leftBarButtonItem = messages;
不过,看起来尺寸在两个方面是错误的。高度永远不会改变,甚至当我改变宽度值时甚至认为宽度会改变,这似乎是不正确的,因为设置了圆角半径,
hasMessageBtn.layer.cornerRadius = 12;
没有完全封闭顶部和底部的圆角。我在这里错过了什么?
When you takes smaller the default size of custom view, its height will stretch the height to
34
. That's why yourleftBarButton
getting distorted.
解法:
将高度和宽度约束应用于
barButton
(建议 *)UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn]; [NSLayoutConstraint activateConstraints:@[[messages.customView.widthAnchor constraintEqualToConstant:24.0], [messages.customView.widthAnchor constraintEqualToConstant:24.0]]];
设置自定义视图的按钮大小,
CGSize(34, 34)
hasMessageBtn.layer.frame = CGRectMake(0, 0, 34, 34); hasMessageBtn.layer.cornerRadius = hasMessageBtn.frame.size.height / 2;
自 iOS11 起,您必须对栏按钮项设置约束。这是它在 objective-c:
中的样子[hasMessageBtn.widthAnchor constraintEqualToConstant:24].active = YES;
[hasMessageBtn.heightAnchor constraintEqualToConstant:24].active = YES;
在swift中:
hasMessageBtn.heightAnchor.constraint(equalToConstant: 24).isActive = true
hasMessageBtn.widthAnchor.constraint(equalToConstant: 24).isActive = true
如果您对此主题有更多疑问,建议您查看 2017 WWDC 视频 Updating Your App for iOS 11