简体   繁体   English

如何删除 UIBarButtonItem 左侧的填充?

[英]how to remove padding left UIBarButtonItem?

I would like to use a custom View as UIBarButtonItem left in an UINavigationController, is it possible to remove the left padding here?我想在 UINavigationController 中使用自定义视图作为 UIBarButtonItem,是否可以在此处删除左填充?

Ill alrady tried:我已经尝试过:

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 ])

在此处输入图片说明

how to remove space left setting icon如何删除剩余空间设置图标

update .更新 tired this code:累了这段代码:

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)

在此处输入图片说明

problem this user when clicked btn setting user and other problem title navigation not center align单击 btn 设置用户和其他问题标题导航未居中对齐时出现此用户问题

You should have noticed that whatever x value you provide to your custom view ie, btn , it will always start from the same position because internally navigation bar doesn't count for position provided for that item and it always set the internally defined position as in below image, I set the background color to the custom button for better visuals.您应该已经注意到,无论您提供给自定义视图的x值,即btn ,它都会始终从相同的位置开始,因为内部导航栏不计入为该项目提供的位置,并且它始终将内部定义的位置设置为下图,我将背景颜色设置为自定义按钮以获得更好的视觉效果。

在此处输入图片说明

So, the trick here is to put your custom button into another container view as below,所以,这里的技巧是将您的自定义按钮放入另一个容器视图中,如下所示,

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]

This will result into this,这将导致这个,

在此处输入图片说明

Now, if you change the x , y , width , height of btn , it will adjust accordingly.现在,如果您更改btnxywidthheight ,它将相应调整。

Please try this.请试试这个。 It works fine for me.这对我来说可以。 If this code not working properly then I will give you another solution.如果这段代码不能正常工作,那么我会给你另一个解决方案。

   let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    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(openSetting), for: .touchUpInside)
    view.addSubview(btn)
    let leftButtonBar = UIBarButtonItem(customView: view)
    self.navigationItem.leftBarButtonItems = ( [leftButtonBar])

Please add other button in View and set the width of view accordingy.It may hepls to you.Thank you请在视图中添加其他按钮并相应地设置视图的宽度。它可能对您有帮助。谢谢

将 cRect 的 x 值设置为零:

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

You can set imageInsets and set image this is example of left margin to set back image on position of default back button您可以设置imageInsets并设置图像这是左边距的示例,用于在默认后退按钮的位置设置后退图像

 let rightBtn = UIBarButtonItem(image: UIImage(named: "BackArrow"), style: .done, target: self, action: #selector(actnClose))
 rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
    
 self.navigationItem.setLeftBarButton(rightBtn, animated: true)

The less painful and actually cleanest solution was to hide the left button and just add a subview directly to the navigationBar and customise it's position and size with the good old constraints.不那么痛苦且实际上最干净的解决方案是隐藏左按钮,然后直接向导航栏添加一个子视图,并使用旧约束自定义它的位置和大小。 Worked like a charm.像魅力一样工作。 Haven't tested it in every situation but seems to work fine.尚未在所有情况下对其进行测试,但似乎工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM