How do i remove text from back button on navbar wi

2019-03-20 08:19发布

I set an arrow custom image to navigation bar by adding the following code in app delegate, it works but now im looking to remove the text completely for back button.

UIImage * backButtonImage = [UIImage imageNamed: @"BackButtonGrey.png"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance]  setBackButtonBackgroundImage: backButtonImage  forState: UIControlStateNormal  barMetrics: UIBarMetricsDefault];

8条回答
啃猪蹄的小仙女
2楼-- · 2019-03-20 09:07

Perfect solution globally

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)

    return true
}
查看更多
太酷不给撩
3楼-- · 2019-03-20 09:13

I just did it like this, worked fine for me :-)

UIImage *backButtonImage = [UIImage imageNamed:@"navigationBarBack.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

[backButton setImage:backButtonImage
            forState:UIControlStateNormal];

backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);

[backButton addTarget:self
               action:@selector(popViewController)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
查看更多
登录 后发表回答