iOS如何把导航默认的返回按钮设置成“返回”

时间:2023-03-08 16:53:06

版权声明:本CSDN博客所有文章不更新,请关注标哥博客:http://www.henishuo.com/

  1. - (void)addBackItemWithAction:(SEL)action {
  2. if (IOS7_OR_LATER) {
  3. UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] init];
  4. returnButtonItem.title = @"返回";
  5. self.navigationItem.backBarButtonItem = returnButtonItem;
  6. } else {
  7. // 设置返回按钮的文本
  8. UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
  9. initWithTitle:@"返回"
  10. style:UIBarButtonItemStylePlain target:nil action:nil];
  11. [self.navigationItem setBackBarButtonItem:backButton];
  12. // 设置返回按钮的背景图片
  13. UIImage *img = [UIImage imageNamed:@"ic_back_nor"];
  14. img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 18, 0, 0)];
  15. [[UIBarButtonItem appearance] setBackButtonBackgroundImage:img
  16. forState:UIControlStateNormal
  17. barMetrics:UIBarMetricsDefault];
  18. // 设置文本与图片的偏移量
  19. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(5, 0)
  20. forBarMetrics:UIBarMetricsDefault];
  21. // 设置文本的属性
  22. NSDictionary *attributes = @{UITextAttributeFont:[UIFont systemFontOfSize:16],
  23. UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetZero]};
  24. [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
  25. }
  26. return;
  27. }