iOS 11 UIBarButtonItem ImageView 大小问题

iOS 11 UIBarButtonItem ImageView size bug

今天早上你好 iOS 11 我发现了很多 iOS 10 没有的错误。我已经纠正了大多数错误,除了两个错误,配置文件图像在右上角

代码按钮:

UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    v.image = img;
    v.layer.masksToBounds = YES;
    v.layer.cornerRadius = 17.5;

    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:v];
    self.navigationItem.rightBarButtonItem = rightBtn;

///// iOS 10 /////

///// iOS 11 /////

我修好了这个 使用以下代码:

[v.widthAnchor constraintEqualToConstant:35].active = YES;

[v.heightAnchor constraintEqualToConstant:35].active = YES;

在 iOS 11 中,您必须需要我们自动布局而不是框架

喜欢下面

    myButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true

我使用 UIbutton 解决了这个问题。

@victor bill 的回答很有魅力,但我想提一下,如果您不先为按钮设置框架,这将不起作用。 设置宽度和高度锚点会使按钮在我的应用程序中完全消失。

v =  [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[v.widthAnchor constraintEqualToConstant:35].active = YES;
[v.heightAnchor constraintEqualToConstant:35].active = YES;

如果您想为您的 image/button/UIView 设置固定尺寸,一般来说,这是 Swift 4 代码:

    yourButton.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
    yourButton.heightAnchor.constraint(equalToConstant: 30.0).isActive = true

您必须使用两个约束手动设置宽度和高度并将它们设置为活动状态。