如何在iPhone的左侧导航栏按钮中给出样式

时间:2011-07-14 08:00:16

标签: iphone uinavigationcontroller uibutton uibarbuttonitem

我在左上角的导航栏中添加了2个带图像的按钮。但我的风格有问题。这是我的按钮截图。这是我在应用程序中编写的按钮代码。

UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 32)];

UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,40,35);
[button1 setBackgroundImage:[UIImage imageNamed: @"remainder.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(sortByremainder) forControlEvents:UIControlEventTouchUpInside];

[viewButtons addSubview:button1];


UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(45,0,60,35);
[button2 setBackgroundImage:[UIImage imageNamed: @"like_symbol.jpg"]  forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(sortByMostFaved) forControlEvents:UIControlEventTouchUpInside];

[viewButtons addSubview:button1];
[viewButtons addSubview:button2];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];

enter image description here

但我希望我的按钮样式应该像这个图像。我希望将图像保留在按钮中,但屏幕下方更具体的按钮。它有边界和凸起区域。

先谢谢你 enter image description here

1 个答案:

答案 0 :(得分:0)

为此你需要使用灰度图像,这看起来会更好。参见附件等示例。

  

enter image description here

enter image description here

enter image description here

您需要使用UIBarButtonItem并在其中设置此类图像,并将该UIBarButtonItem指定给self.navigationItem.leftBarButtonItem。

另外,像下面一样使用BarButtonItemStyle边框,这样你就可以得到它。

UIBarButtonItem *btnSetting = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(setting:)];

self.navigationItem.rightBarButtonItem = btnSetting;

希望这有帮助。