我试图为我的应用程序的导航栏设置自定义后退按钮。现在我正在使用此代码来执行此操作:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
这最终会扭曲我的形象并使其延伸。它还在其上添加了文本。 我该怎么做才能显示这张图片?
以下是图片:http://thai-flashcards.info/images/arrow-blue-rounded-left.jpg?1303347394
答案 0 :(得分:2)
您正在寻找的是可伸缩的图像。
这应该让你整理出来:
http://idevrecipes.com/2010/12/08/stretchable-images-and-buttons/
编辑:查看您的图片我认为这不起作用。
您将需要制作自定义UIButton并将后退按钮设置为该自定义按钮并自行处理ViewController弹出窗口。
您可以像这样设置/设置按钮。
UIImage *buttonImage = [UIImage imageNamed:@"mybuttonimage.png"];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton addTarget:self action:@selector(goBack)forControlEvents:UIControlEventTouchUpInside];
[doneButton setImage:buttonImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:doneButton];
self.navigationItem.leftBarButtonItem = backButton;
你需要将你的方法添加到goBack
-(void)goBack { // Go Back
[self.navigationController popToViewController:
[self.navigationController.viewControllers objectAtIndex:3] animated:YES];
}
答案 1 :(得分:2)
在 didFinishLaunchingWithOptions 下添加AppDelegate
UIImage *backBtnIcon = [UIImage imageNamed:@"back.png"];
UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault ];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -30) forBarMetrics:UIBarMetricsDefault];