【发布时间】:2018-01-12 21:45:04
【问题描述】:
我尝试添加带有自定义图像的导航栏按钮。但是,无论我使用什么方法,图像总是显得拉伸。
方法一:
let barbuttonitem = UIBarButtonItem(image: UIImage(named: "apps_small"), style: .plain, target: self, action: nil)
navigationItem.leftBarButtonItem = barbuttonitem
看起来像这样:
方法二:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.addTarget(self, action: #selector(TeachingRootViewController.appsTouched), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
看起来像这样:
方法三:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "apps_small"), for: .normal)
button.setTitle("Button", for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
button.imageEdgeInsets = .init(top: 5, left: 5, bottom: 5, right: 300)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem?.imageInsets = .init(top: 5, left: 5, bottom: 5, right: 300)
看起来像这样:
如你所见,标题不见了。
在 UI Hierarchy 中,如下所示:
按钮似乎占据了导航栏中的所有空间。
但是,带有系统项的按钮没有问题:
有谁知道这个问题的原因吗?我想我的想法已经用完了。
【问题讨论】:
-
你解决了这个问题吗?
标签: ios image button uinavigationbar uinavigationitem