如何为UIBarButtonItem设置图像灵活空间 - IOS

时间:2013-05-06 10:28:36

标签: iphone ios cocoa-touch uibarbuttonitem uitoolbar

看看这张图片......

enter image description here

我想设计这样的工具栏,每个UIBarButtonItem应该用细线分隔。所以我在每个UIBarButtonItem之间插入了灵活的空格按钮(这有助于我在方向改变时对齐UIBarButtonItem),但是如果我为它设置了一个图像,它不再作为灵活的按钮,则更改为普通的UIBar按钮。

所以我需要一个带图像的灵活空间按钮..不知道怎么做。 (要么) 如何设计如上所述的UIToolbar ......真的很感激任何帮助。感谢名单

2 个答案:

答案 0 :(得分:0)

设置UIBarbuttonItem的背景图像

UIImage *faceImage = [UIImage imageNamed:@"facebook.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];

答案 1 :(得分:0)

我认为这会对你有帮助..

UIImage* topImage = [UIImage imageNamed:@"TabBarGradient.png"];

// Create a new image context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, topImage.size.height*2), NO, 0.0);

// Create a stretchable image for the top of the background and draw it
UIImage* stretchedTopImage = [topImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[stretchedTopImage drawInRect:CGRectMake(0, 0, width, topImage.size.height)];

// Draw a solid black color for the bottom of the background
[[UIColor blackColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, topImage.size.height, width, topImage.size.height));

// Generate a new image
UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return resultImage;

有关详细信息,请查看此内容...

http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/

如果没有解决你的问题..我知道..