简体   繁体   English

导航栏项目拉伸按钮的图像

[英]Navigation Bar Item stretching image for button

I have setup a Navigation Bar in a storyboard with an UIBarButton for the right. 我在情节提要中设置了一个导航栏,右边带有一个UIBarButton。

在此处输入图片说明

This is the code I use to update the image for this: 这是我用来为此更新映像的代码:

// Setup the right navigation bar item
[self.addGameButton setBackgroundImage:[UIImage imageNamed:@"addGameButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addGameButton setTitle:@""];

The image is here, it is 76x58 (at 2x). 图像在这里,是76x58(2倍)。 It is 38x29 (at normal). 它是38x29(正常)。 在此处输入图片说明

When run on the device the image is stretching and I do not know why? 在设备上运行时,图像正在拉伸,我不知道为什么? 在此处输入图片说明

The backgroundImage property is meant to draw a stretchable image for the background of the button. backgroundImage属性用于为按钮的背景绘制可拉伸的图像。 If you want the image inside the button, you could try initializing the UIBarButtonItem with this: 如果要将图像放在按钮内,则可以尝试使用以下方法初始化UIBarButtonItem:

UIImage *image = [UIImage imageNamed:@"images/global/add"];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setRightBarButtonItem:barButtonItem];

For you, it looks like you have an image that you want to replace the whole button with. 对您来说,您似乎有一张要替换整个按钮的图像。 You could also try this code, which will create a custom button as the view for your UIBarButtonItem: 您也可以尝试以下代码,该代码将创建一个自定义按钮作为UIBarButtonItem的视图:

UIImage *addImage = [UIImage imageNamed:@"images/global/add"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(0, 0, addImage.size.width, addImage.size.height)];
[addButton setBackgroundImage:addImage forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setRightBarButtonItem:barButtonItem];

在情节提要中,您可以将UIButton拖到UIBarButtonItem上,并且可以根据需要自定义UIButton。

尝试将按钮的框架设置为所需的大小。

Stuart, since you have setTitle:@"" the nav bar button will not display the text "add" but instead take up the space that was allocated to the text.. I am also yet to find a proper solution but a workaround for your problem is to set the font size to 0.1f. Stuart,由于您具有setTitle:@“”导航栏按钮将不会显示文本“ add”,而是占用分配给文本的空间。.我也尚未找到合适的解决方案,但可以解决您的问题问题是将字体大小设置为0.1f。 This will resolve your issue.. 这样可以解决您的问题。

you need to set the navigation bar appearance in the "application:didFinishLaunchingWithOptions” method of AppDelegate.m, try adding the following code: 您需要在AppDelegate.m的“ application:didFinishLaunchingWithOptions”方法中设置导航栏外观,请尝试添加以下代码:

UIImage *buttonback = [[UIImage imageNamed:@"nav_back"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 22, 0, 0)];

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

    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor whiteColor],
UITextAttributeTextColor, [UIFont fontWithName:@"verdana" size:0.1f],
UITextAttributeFont, nil] forState:UIControlStateNormal];

Also Note that this will work with iOS 6, looking for a solution for compatibility in iOS 5 另请注意,这将与iOS 6一起使用,以寻找iOS 5兼容性的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM