【问题标题】:iOS 11 UIBarButtonItem ImageView size bugiOS 11 UIBarButtonItem ImageView 大小错误
【发布时间】:2017-09-25 11:29:14
【问题描述】:

今天早上你好 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 /////

【问题讨论】:

    标签: ios image size uinavigationbar ios11


    【解决方案1】:

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

    如下图

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

    【讨论】:

    • 非常感谢,第一个uitableviewcell的padding问题怎么解决?我使用这个库进行标题滚动 GSKStretchyHeaderView
    【解决方案2】:

    我解决了这个问题 使用以下代码:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          如果你想为你的图像/按钮/UIView 设置一个固定大小,这是 Swift 4 代码:

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

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

          【讨论】: