我想设置自定义图像后退按钮时,一个新的观点被压入堆栈,可自动获得地方上的导航条。
我曾尝试加入父的viewController的viewDidLoad中的以下情况:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
我也曾尝试以下操作:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BackButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = btn;
使用UIAppearance产生相当奇怪的结果:
Answer 1:
试试这个代码
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
然后这样定义GoBack方法
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
Answer 2:
由于iOS的7.0存在API的新方法backIndicatorImage
。 你可以用它代替setBackButtonBackgroundImage
,如果你不想要的形象被拉伸(如果你想有一个固定大小的自定义后退箭头例如)以适合文本做。 下面是SWIFT一个例子:
let image = UIImage(named: "back_button")
UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
您可以隐藏使用这一招的按钮上的文本:
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default)
Answer 3:
这是我使用的代码,并在自己的iOS 5应用程序完美的作品。 此代码是从application:didFinishLaunchingWithOptions:
在应用程序的委托:
UIImage * backButtonImage = [UIImage imageNamed: @"back-button-image"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
您将可能需要使用可拉伸的图像,在面临着“点”左边的是左盖
或者斯威夫特
let backButtonImage = UIImage(named: "back-button-image")
backButtonImage = backButtonImage?.stretchableImageWithLeftCapWidth(15, topCapHeight: 30)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)
Answer 4:
的iOS 8+斯威夫特版本(可转换为ObjC没有太大的努力)。 这种方法保留了所有的优势和相关标准的后退按钮(上推/弹出,互动的流行姿态动画等),导航栏的行为
// Call the code ONE TIME somewhere on app launch to setup custom back button appearance
UINavigationBar.appearance().tintColor = UIColor.blackColor()
// If you need custom positioning for your back button, in this example button will be 1 px up compared to default one
// Also only vertical positioning works, for horizontal add offsets directly to the image
let backImageInsets = UIEdgeInsetsMake(0, 0, -1, 0)
// Get image, change of rendering (so it preserves offsets made in file), applying offsets
let backImage = UIImage(named: "YourButtonImageAssetFileName")?.imageWithRenderingMode(.AlwaysOriginal).imageWithAlignmentRectInsets(backImageInsets)
// Setting images
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
// Call EACH TIME BEFORE pushing view controller if you don't need title near your back button arrow
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
// Push controller
self.navigationController?.pushViewController(vc, animated: true)
Answer 5:
尝试下面的代码,它为我工作:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
Answer 6:
这为我工作:
let button1 = UIBarButtonItem(
image: #imageLiteral(resourceName: "back_arrow"),
style: .plain,
target: self,
action: #selector(self.backBtnTapped(_:))
)
self.navigationItem.leftBarButtonItem = button1
Answer 7:
此代码工作了雨燕4和XCode的9.4:
该代码添加到AppDelegate中的func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
以适用于所有视图控制器:
//Custom back button
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
let backImg: UIImage = UIImage(named: "back")!
barButtonItemAppearance.setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)
let image = UIImage()
UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
文章来源: Setting a Custom Image for a Back Button on UINavigationBar