3

I need to create a navBar back button in accordance to the designer's plan. The back button has a pattern image and stitched leather on the perimeter.

Here it is:

enter image description here

My question is it possible to create this without a great amount of hassle and headache? Or if it's possible at all, since the back button has varying width?

Thanks!

UPDATE

Alright, with the help of PartiallyFinite turns out this is very easy. If you set the UIEdgeInsets correctly it will keep the left side fixed, the right side fixed, and then duplicate the middle of the image considering the back button's width.

This is the image I used for my back Button:

enter image description here

And these are my inset settings. You can try them yourself:

backButtonImage = [backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 17, 5, 12)];

Hope this helps someone in the future.

4

5 回答 5

4

您需要为按钮提供可拉伸的图像,以便它知道如何正确显示它:

UIImage *buttonImage = [[UIImage imageNamed:@"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)]

您不需要对图像本身做任何特殊的事情,但您确实需要为可调整大小的图像指定适当的边缘插图,以指示不应拉伸的图像边缘周围的区域,如上所示(示例显示左侧 15 个像素和右侧 6 个像素的插图)。该区域应覆盖箭头和弯曲的右边缘,以便可以根据需要拉伸中间区域。阅读文档以获取有关此方法的更多信息。

更新:默认情况下,图像的可调整大小区域将平铺到新大小,但是如果您想让它拉伸,您可以使用resizableImageWithCapInsets:resizingMode:并传递UIImageResizingModeStretch来实现该行为。对于您的情况,显然平铺更好,因为它保留了拼接,但对于某些背景图像,拉伸是更好的解决方案。只是把它放在这里是为了帮助任何在未来看到它的人。

获得可拉伸图像后,您可以使用以下代码更改后退按钮的外观:

[myBackButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

或者,您可以使用以下代码为应用中的所有后退按钮设置此自定义外观:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

当你的应用启动时运行它,它将影响你应用中的所有后退按钮。

请注意,与其他一些答案的建议相反,您不需要手动创建任何后退按钮。如果您创建UINavigationController并以推荐的方式使用它(请阅读文档中的相关内容,当您使用 推送视图控制器时,将为您创建一个导航栏和后退按钮pushViewController:animated:。如果您使用全局UIAppearance代码片段来应用自定义按钮样式,它将自动应用于您拥有的所有后退按钮。

您可以setBackButtonBackgroundImage:forState:barMetrics: 在官方文档中了解更多信息。

网上也有许多教程可以更深入地解释它是如何工作的以及如何做到这一点,这里有一些很好的:

于 2013-06-03T11:53:12.710 回答
0

您需要创建自己的 BarButtonItem。您不能为系统后退按钮设置图像。

 self.navigationItem.leftBarButtonItem =  [[[UIBarButtonItem alloc] initWithCustomView:buttonWithYourImage] autorelease];
于 2013-06-03T11:53:22.180 回答
0

您可以创建自己的 UIBarButtonItem 并将其设置为当前视图控制器导航栏上的 leftButtonItem:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"your_image"] style:UIBarButtonItemStyleBordered target:self action:@selector(yourFunction:)];
self.navigationItem.leftBarButtonItem = btn;
于 2013-06-03T11:53:37.797 回答
0

我在我的 Appdelegate.m 中使用它来自定义按钮。(“buttonBack”是图像的名称)。希望它可以帮助你

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
UIImage *barButtonImage = [[UIImage imageNamed:@"buttonBack"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

return YES;
}
于 2013-06-03T12:03:41.680 回答
0

首先,您需要制作自己的后退按钮..并将该图像设置为后退按钮。

UIBarButtonItem * btn=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(changed)];

self.navigationItem.leftBarButtonItem=btn;

[btn setImage:[UIImage imageNamed:@"yourimagename"]];


-(void)changed
{


    [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];

}

试试这个对你真的很有帮助...

于 2013-06-03T12:04:27.893 回答