在我的应用中创建自定义后退按钮

时间:2017-08-08 02:01:25

标签: ios swift uinavigationcontroller uibarbuttonitem

我正在尝试在我的应用中更改导航控制器后退按钮的默认外观。

我想使用和图像(图标)并删除“后退”文本。 此代码在整个按钮上拉伸图像,但不删除按钮标题。

let backImg: UIImage = UIImage(named: "icon_back")!
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)

如何做的建议是在AppDelegate(didFinishLaunchingWithOptions)中?

2 个答案:

答案 0 :(得分:1)

简单的方法就是为您的项目创建一个BaseViewController,该UIViewController派生自BaseViewController。您可以在custom leftBarButton中使用常用方法在每个viewController中创建derived from this BaseViewController。项目中剩余的viewController应为class BaseViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() createLeftBarButton(image: #Pass image here#, width: #Pass width of your image view#) // Create custom back bar button. } /**Create cutom back bar button*/ func createLeftBarButton(image: UIImage?, width: CGFloat) { let backButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: 50)) backButton.imageView?.contentMode = .scaleAspectFill backButton.imageView?.bounds = CGRect(x: 0, y: 0, width: width, height: width) backButton.setImage(image, for: .normal) backButton.setImage(image, for: .highlighted) backButton.addTarget(self, action: #selector(leftBarButtonItemPressed(_:)), for: .touchUpInside) let leftItem: UIBarButtonItem = UIBarButtonItem(customView: backButton) navigationItem.leftBarButtonItem = leftItem } /**Custom back bar button pressed. So handle here*/ func leftBarButtonItemPressed(_ sender: UIButton) { view.endEditing(true) // End editing if any. if isViewControllerPresented() { // Check view controller is presented or pushed dismiss(animated: true, completion: nil) // Dismiss ViewController if presented } else { _ = navigationController?.popViewController(animated: true) // Pop ViewController if pushed } } /**To check whether view controller is presented or pushed.*/ func isViewControllerPresented() -> Bool { if self.presentingViewController?.presentedViewController == self { return true } if (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController) && self.navigationController?.viewControllers.count == 1 { return true } if self.tabBarController?.presentingViewController is UITabBarController { return true } return false } } // Sub class your remaining viewControllers like this. class FirstViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() // When calling this super method, the custom back bar button will be created for you } }

{{1}}

感谢。

答案 1 :(得分:0)

您可以通过设置navigationItem' BackButtonBackgroundImage;

来完成此操作

您也可以设置全局效果的外观:

let item = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]) item.setBackButtonBackgroundVerticalPositionAdjustment(-10, for: .default) item.setBackButtonBackgroundImage(youImage, for: .normal, barMetrics: .default)

删除后面的标题,你可以这样做:

@implementation UINavigationItem (BackItem)

-(UIBarButtonItem *)backBarButtonItem { UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil]; item.tintColor = [UIColor darkGrayColor]; return item; }

@end