if #available(iOS 11, *) {
let view = UIView()
view.frame = CGRect(x: 20, y: 0, width: 70, height: 50)
let btn: UIButton = UIButton(frame: CGRect(x:-14, y: 0, width: 30, height: 27))
btn.setBackgroundImage(UIImage(named: "BackArrow"), for: UIControlState());
btn.addTarget(self, action: #selector(ExampleController.barBtnBackAction), for: UIControlEvents.touchUpInside)
btn.center = CGPoint(x: btn.center.x,y :view.center.y)
let tap = UITapGestureRecognizer(target: self, action: #selector(ExampleController.barBtnBackAction))
view.addGestureRecognizer(tap)
view.isUserInteractionEnabled = true
view.backgroundColor = UIColor.black
view.addSubview(btn)
let leftButtonItem = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItem = leftButtonItem
}
I am using this code to set back button in navigation bar in iOS 11 but it's not working good ? user has problem in tapping back button I cant use default as I want custom action with it and before iOS 11 i was using this code :- (Given Below for iOS 10) It was working fine.
let barBtnNeg = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: self, action: nil)
barBtnNeg.width = -15.0
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 27))
btn.setBackgroundImage(UIImage(named: "BackArrow"), for: UIControlState());
btn.addTarget(self, action: #selector(ExampleController.btnBackAction), for: UIControlEvents.touchUpInside)
let barBtnBack = UIBarButtonItem(customView: btn)
self.navigationItem.leftBarButtonItems = [barBtnNeg, barBtnBack]
And if I use this code the back arrow image gets shifted towards right (alignment error) and if I decrease the x axis of the frame still image is not shifted so i have to use above code (if #available(iOS 11, *)).
So any solution to fix back button problem (Navigation Bar) With Custom image, Custom Selector(action) and custom alignment?
Add this class for navigation controller
class NavigationBar: UINavigationBar {
override func layoutSubviews() {
super.layoutSubviews()
for subview in self.subviews {
let stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("BarBackground") {
subview.frame.origin.y = -20
subview.frame.size.height = 64
}
}
}
}
create navigation bar where u need
let navigationBar = NavigationBar(frame: CGRect(origin: CGPoint(x: 0,
y: 20),
size: CGSize(width: view.frame.width,
height: 40)))
navigationBar.backgroundColor = UIColor(red: 46.0/255.0, green: 135.0/255.0, blue: 245.0/255.0, alpha: 1.0)
navigationBar.isTranslucent = false
let button1 = UIButton()
button1.setImage(UIImage(named: "Back"), for: UIControlState())
button1.addTarget(self, action: #selector (goBack(_:)), for: UIControlEvents.touchUpInside)
button1.frame = CGRect(x: 0, y: 5, width: 20, height: 20)
let barButton1 = UIBarButtonItem(customView: button1)
self.navigationItem.leftBarButtonItem = barButton1
navigationBar.items = [navigationItem]
self.view .addSubview(navigationBar)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.