[英]iOS 11 back button issue
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 ? 我正在使用此代码在iOS 11的导航栏中设置后退按钮,但效果不佳? 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. 用户在点击后退按钮时遇到问题,我无法使用默认值,因为我想使用它进行自定义操作,并且在iOS 11之前,我正在使用以下代码:-(针对iOS 10,请参见下面),它工作正常。
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, *)). 如果我使用此代码,则后退箭头图像会向右移动(对齐错误),并且如果我减小帧的x轴,则静止图像不会移动,因此我必须使用上述代码(如果#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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.