提问人:iOSDude 提问时间:12/15/2021 更新时间:12/15/2021 访问量:115
如何在 Objective C iOS 中显示与 backBarButtonItem 相同的 leftBarButtonItem 外观
How to display leftBarButtonItem look same as backBarButtonItem in Objective C iOS
问:
我有一个用于几个视图控制器的 backBarButtonItem,默认情况下显示后退箭头和文本,我想为其他一些视图控制器创建一个 leftBarButtonItem(它们被呈现而不是推送)。
如何在目标 C 中创建与 backBarButtonItem 相同的 leftBarButtonItem
backBarButtonItem Implemention 和 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;
遵循了一些其他链接,但没有按预期工作,任何人都可以提出解决方案。
答:
1赞
DonMag
12/15/2021
#1
您可以创建一个按钮并使用自己的图像或系统图像,然后将该按钮设置为条形按钮项的自定义视图。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];
顺便说一句:在错误的地方使用“标准按钮”可能会给用户带来困惑。通常,在显示控制器时,用户希望看到“确定”或“完成”或“保存”等按钮,以及(通常)“取消”按钮。显示“后退”按钮,看起来它应该像正常导航一样运行,最终可能会变得模棱两可。
评论