如何删除左侧UIBarButtonItem的填充?

时间:2018-07-19 06:06:06

标签: ios swift uinavigationcontroller

我想使用自定义视图作为UINavigationController左侧的UIBarButtonItem,是否可以在此处删除左侧填充?

生病的阿拉迪尝试过

let  btn = UIButton()
btn.frame = CGRect(x: -50, y: -50, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)

let leftButtonBar = UIBarButtonItem(customView: btn)

self.navigationItem.leftBarButtonItems = ( [leftButtonBar , otherBtn ])

enter image description here

如何删除剩余空间设置图标

更新。这段代码很累:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 
view.backgroundColor = .gray
let  btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(DashboardTabBarController.openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)

enter image description here

单击btn设置用户时出现此用户问题 和其他问题标题导航不居中对齐

5 个答案:

答案 0 :(得分:3)

您应该已经注意到,无论您提供给自定义视图的x值是什么,btn都将始终从同一位置开始,因为内部导航栏不会计入为此提供的位置项,并且它始终将内部定义的位置设置为下图所示,我将背景色设置为自定义按钮,以获得更好的视觉效果。

enter image description here

因此,这里的技巧是将自定义按钮放入另一个容器视图,如下所示,

let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
view.backgroundColor = .green
let  btn = UIButton(frame: CGRect(x: -10, y: 10 , width: 30, height: 30))
btn.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
btn.backgroundColor = .yellow
btn.tintColor = .red
view.addSubview(btn)

let  btn2 = UIButton(frame: CGRect(x: 20, y: 10, width: 30, height: 30))
btn2.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn2.contentMode = .scaleAspectFit
btn2.tintColor = .yellow
btn2.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn2)

let itemsContainer = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = [itemsContainer]

这将导致这种情况,

enter image description here

现在,如果您更改x中的ywidthheightbtn,它将进行相应的调整。

答案 1 :(得分:1)

请尝试这个。这对我来说可以。如果此代码无法正常工作,那么我将为您提供另一种解决方案。

Checked items:
  a
  b
  c
Remaining items:
  d
  e
  f
  g

请在“视图”中添加其他按钮,然后根据需要设置视图的宽度。它可能会帮助您。谢谢

答案 2 :(得分:0)

将cRect的x值设置为零:

btn.frame = CGRect(x: 0, y: -50, width: 44, height: 50)

答案 3 :(得分:0)

您可以设置abstract class AuthenticationRepository { /// The current user. User get currentUser; /// True if logged in. bool get loggedIn; /// Logs in, saving the [User]. Future<void> login(AuthenticationParams params); /// Logs out, disposing of the [User]. Future<void> logout(); /// Same as [logout], but logs out of all devices. Future<void> logoutAll(); /// Retrieves stored login credentials. Future<AuthenticationParams> retrieveStoredCredentials(); } 并设置图像,这是左边距示例,用于在默认后退按钮的位置上设置后退图像

imageInsets

答案 4 :(得分:0)

不那么痛苦且实际上最干净的解决方案是隐藏左按钮,然后直接向导航栏添加一个子视图,并使用旧约束自定义它的位置和大小。像魅力一样工作。尚未在所有情况下对其进行测试,但似乎工作正常。