如何显示 leftBarButtonItem 与 Objective C iOS 中的 backBarButtonItem 相同

How to display leftBarButtonItem look same as backBarButtonItem in Objective C iOS

我有几个视图控制器的 backBarButtonItem,它默认显示后退箭头和文本我想为其他一些视图控制器(显示而不是推送)创建一个 leftBarButtonItem。

如何创建一个 leftBarButtonItem 应该看起来与 Objective C

中的 backBarButtonItem 一样

backBarButtonItem 实现和 ViewController 被推送。

    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self
                                                                     action:nil];
NSString *accentContrastcolorString = [NSUserDefaults.standardUserDefaults valueForKey:@ "AccentcontrastColor"];
UIColor *accentContrastcolor = [self colorFromHexCode:accentContrastcolorString];
backNavigationItem.tintColor = accentContrastcolor;
viewController.navigationItem.backBarButtonItem = backNavigationItem;

我创建了如下所示的 leftBarButtonItem,它显示后退标题需要具有相同大小的左箭头箭头。

        UIBarButtonItem *bacNavigationItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(backButtonTapped:)];
    NSString *accentContrastcolorString = [NSUserDefaults.standardUserDefaults valueForKey:@ "AccentcontrastColor"];
    UIColor *accentContrastcolor = [self colorFromHexCode:accentContrastcolorString];
    bacNavigationItem.tintColor = accentContrastcolor;
    viewController.navigationItem.leftBarButtonItem = bacNavigationItem;

访问了一些其他链接但未按预期工作,任何人都可以提出解决方案。

How to make leftBarButtonItem looking like backBarButtonItem?

您可以创建一个按钮并使用您自己的图像或 chevron.backward 系统图像,然后将该按钮设置为条形按钮项目的自定义视图。

例如:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@"Back" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:17.0]];

UIImageSymbolConfiguration *cfg = [UIImageSymbolConfiguration configurationWithPointSize:17.0 weight:UIImageSymbolWeightSemibold];
UIImage *img = [UIImage systemImageNamed:@"chevron.backward" withConfiguration:cfg];
[btn setImage:img forState:UIControlStateNormal];

// add a little spacing between button image and title
CGFloat offset = 8;
UIEdgeInsets edge = btn.contentEdgeInsets;
edge.right = offset;
btn.contentEdgeInsets = edge;
edge = btn.titleEdgeInsets;
edge.right = -offset;
btn.titleEdgeInsets = edge;
edge = btn.imageEdgeInsets;

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

附带说明:在错误的位置使用“标准按钮”可能会给用户带来困惑。通常,当 呈现 控制器时,用户希望看到诸如“确定”、“完成”或“保存”之类的按钮,以及带有(通常)“取消”按钮。显示一个“后退”按钮,看起来应该像正常导航一样运行,但最终会变得模棱两可。