我可以执行操作,一般在所有视图上设置导航后退按钮图标。例如,如果我将代码放入App Delegate方法,那么它将用于所有控制器。
答案 0 :(得分:3)
这是我在当前应用程序中使用的最终答案,希望它有用..
查看DidLoad方法..
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, 40, 40);
[leftButton setImage:[UIImage imageNamed:@"Back"] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(left_menu_click:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
添加此方法以导航popViewController ..
-(IBAction)left_menu_click:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
答案 1 :(得分:1)
您可以在整个应用程序中尝试使用正确的图像显示以下按钮。
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
答案 2 :(得分:1)
在didFinishLaunchingWithOptions
class:
AppDelegate
方法中添加以下代码
// Following line is for tint color that is optional, You can add if you wants to change color of back button
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
如果您想要更改背景图片,那么
// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"button_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
答案 3 :(得分:1)
您可以尝试这样的自定义按钮:
+ (UIBarButtonItem *)backButtonForTarget:(id)target
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:target action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(0, 0, 70, 44)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(11, -10, 8, 35)];
[btn setImage:[UIImage imageNamed:@"topbar_back.png"] forState:UIControlStateNormal];
UIBarButtonItem *barbtnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barbtnItem;
}
将此按钮添加为leftBarButtonItem
self.navigationItem.leftBarButtonItem = [UtilityClass backButtonForTarget:self];
答案 4 :(得分:0)
您应该像这样修改UINavigationBar
:
let appearance = UINavigationBar.appearance()
appearance.backIndicatorImage = image
appearance.backIndicatorTransitionMaskImage = image
如果要删除默认标题,请添加:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
查看推送到导航控制器的视图控制器。
您可以在Apple docs
中找到更多信息答案 5 :(得分:0)
只需使用外观()找到解决方案
快速
let backImage = UIImage(named: "back")
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
UIColor.white], for: .normal)
UIBarButtonItem.appearance().tintColor = UIColor.green
答案 6 :(得分:0)
您可以尝试
let backImage = UIImage(named: "icon")
let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
appearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
appearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets,
for: .normal,
barMetrics: .default)
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
for: .normal)
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
for: .highlighted)