我试图用自定义图片替换UINavigationController
BackBarButtonItem
,我希望其中没有像这样的后退图标:
我从另一个堆叠的帖子中拍摄这张照片,但我的事情发生了类似的情况
此处列出了解决方法:Remove back arrow in iOS7
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"]
style:UIBarButtonItemStylePlain
target:nil
action:nil];
if ([UINavigationBar instancesRespondToSelector:@selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
}
问题是我无法将此代码转换为Swift。如果有人能帮助我,我将不胜感激。提前谢谢!
答案 0 :(得分:2)
获取向后滑动功能:
首先在viewDidLoad
中设置代理:
self.navigationController!.interactivePopGestureRecognizer.delegate = self
然后在推送时禁用手势:
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
self.interactivePopGestureRecognizer.isEnabled = false
}
并在viewDidDisappear
中启用:
self.navigationController!.interactivePopGestureRecognizer.isEnabled = true
答案 1 :(得分:0)
使用以下代码:
//Hide Default Back Button First
self.navigationItem.setHidesBackButton(true, animated:true);
//Your code to show back button
let backButton = UIButton()
backButton.setImage(UIImage(named: "imagename"), forState: .Normal)
backButton.frame = CGRectMake(0, 0, 30, 30)
backButton.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = backButton
self.navigationItem.leftBarButtonItem = rightBarButton
//Back Button Action
func action()
{
self.navigationController?.popViewControllerAnimated(true)
}
答案 2 :(得分:0)
在swift 3中使用以下代码:
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.setBackButton()
}
func setBackButton() {
let backImage = UIImage(named:"back_icon") as UIImage!
let backButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.plain, target: self, action: #selector(BaseViewController.viewWillDisappearC) )
self.navigationItem.leftBarButtonItem = backButton
}
func viewWillDisappearC() {
_ = self.navigationController?.popViewController(animated: true)
}
}
答案 3 :(得分:0)
在swift 2.2(xocde 7.3)中使用以下代码:
类BaseViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.setBackButton()
}
func setBackButton() {
let backImage = UIImage(named:"back_icon") as UIImage!
let backButton = UIBarButtonItem(image: backImage, style:UIBarButtonItemStyle.Plain, target: self, action: #selector(BaseViewController.viewWillDisappearC) )
self.navigationItem.leftBarButtonItem = backButton
}
func viewWillDisappearC() {
self.navigationController?.popViewControllerAnimated(true)
}
}