我想使用自定义视图 作为 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 ])
Run Code Online (Sandbox Code Playgroud)
如何删除剩余空间设置图标
更新。厌倦了这段代码:
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) …Run Code Online (Sandbox Code Playgroud) 我想在可见性GONE和VISIBLE. 这在Android开发中确实很容易实现,但我不知道如何在Swift中使用相同的方法
我尝试使用此代码将标签设置为消失:
// set the width constraint to 0
let widthConstraint = NSLayoutConstraint(item: self.labelShortDescription, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
self.labelShortDescription.addConstraint(widthConstraint)
// set the height constraint to 0
let heightConstraint = NSLayoutConstraint(item: self.labelShortDescription, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 0)
self.labelShortDescription.addConstraint(heightConstraint)
Run Code Online (Sandbox Code Playgroud)