在UINavigationBar上为后退按钮设置自定义图像
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在UINavigationBar上为后退按钮设置自定义图像相关的知识,希望对你有一定的参考价值。
我正在尝试为后退按钮设置自定义图像,当新视图被压入堆栈时,该按钮会自动进入导航栏。
我尝试在父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会产生非常奇怪的结果:
试试这段代码
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];
}
从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)
这是我正在使用的代码,并且在我自己的iOS 5应用程序中完美运行。此代码来自app delegate中的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)
iOS 8+ Swift版本(可以毫不费力地转换为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)
试试下面的代码,它对我有用:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
这对我有用:
let button1 = UIBarButtonItem(
image: #imageLiteral(resourceName: "back_arrow"),
style: .plain,
target: self,
action: #selector(self.backBtnTapped(_:))
)
self.navigationItem.leftBarButtonItem = button1
此代码适用于Swift 4和XCode 9.4:
将此代码添加到AppDelegate qazxsw poi以应用于所有视图控制器:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
以上是关于在UINavigationBar上为后退按钮设置自定义图像的主要内容,如果未能解决你的问题,请参考以下文章
iPhone - 如何使用我的纹理背景自定义 UINavigationBar 后退按钮