【问题标题】:Navigation bar custom image view issue in iOS 11iOS 11 中的导航栏自定义图像视图问题
【发布时间】:2017-10-06 10:56:05
【问题描述】:

我正在将普通图像视图设置为我的导航栏项目的自定义视图,但这就是正在发生的事情:

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 34, height: 34))
imageView.kf.setImage(with: user.profilePictureURL)
if user.profilePictureURL == nil {
    imageView.image = #imageLiteral(resourceName: "ProfilePlaceholderSuit")
}
imageView.backgroundColor = .white
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 17
imageView.layer.borderWidth = 1
imageView.layer.borderColor = UIColor.white.cgColor
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: imageView)
print("ok, so the image view frame is", imageView.frame) // 0, 0, 34, 34

【问题讨论】:

    标签: ios swift ios11


    【解决方案1】:

    由于在 iOS 11 中 UIBarButtonItem 使用自动布局而不是框架,许多开发人员面临此类问题。

    所以你只想在右上角的 barButton 项目上设置个人资料图像然后please check my answer.

    否则,您可以在代码中添加 imageView 的约束,如下所示

        let widthConstraint = imageView.widthAnchor.constraint(equalToConstant: 34)
        let heightConstraint = imageView.heightAnchor.constraint(equalToConstant: 34)
        heightConstraint.isActive = true
        widthConstraint.isActive = true
    

    【讨论】:

      【解决方案2】:

      这是 Kingfisher 框架的问题。我已切换到 SDWebImage,它工作正常(有时)。

      编辑:

      成功了

      let imageView = UIImageView()
      imageView.translatesAutoresizingMaskIntoConstraints = false
      imageView.heightAnchor.constraint(equalToConstant: 34).isActive = true
      imageView.widthAnchor.constraint(equalToConstant: 34).isActive = true
      

      【讨论】:

      • 不,Kingfisher 框架没有问题,这是 iOS 11 的问题,您应该使用自动布局而不是框架
      • 感谢@iPatel。我必须将图像视图限制在导航栏上还是只设置它的高度和宽度?
      • 这行得通@iPatel。随时发布作为答案
      • 但是为什么你使用图像视图并添加 UITapGestureRecognizer 甚至 UIButton 都提供内置功能?
      • 所以我可以使用 Kingfisher 轻松设置图像视图的图像,但是很好。
      猜你喜欢
      • 2018-02-22
      • 2018-04-14
      • 1970-01-01
      • 2017-11-07
      • 2019-05-08
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多