Since iOS 11, just setting frame for customView
and not UIBarButtonItem
won't work (like suggested in accepted answer). It seems like adding Autolayout to UIBarButtonItem
overrides the set frame.
This post gave me the idea:
navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0
let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)
yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])
Also note, that it is preferred that you use UIButton
as your CustomView
as you have to rely on it to trigger the action.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…