我正在开发一个小应用程序。我需要在子类中创建三个按钮。一个按钮是add
,另一个是search
,最后一个是back
。我还创建左右按钮。但我无法在导航栏的中心创建搜索按钮。我该如何创建它?我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
[flipButton release];
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc]
initWithTitle:@"Add"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addbuttonview)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
}
如何在导航栏中创建中间按钮?请帮帮我。
答案 0 :(得分:6)
以下是以编程方式在导航栏中使用段控件的代码
NSArray* arr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"Log_Button.png"], [UIImage imageNamed:@"Chart_Button.png"], nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:arr];
[segmentedControl addTarget:self action:@selector(action) forControlEvents:UIControlEventValueChanged];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[arr release];
UIBarButtonItem *rb = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rb];
[rb release];
答案 1 :(得分:3)
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10, 4, 100, 50);
[btnBack setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"button_back" ofType:@"png"]] forState:UIControlStateNormal];
[btnBack addTarget:self action:@selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnBack];
UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
btnHome.frame = CGRectMake(115, 4, 38, 30);
[btnHome setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"header_icon_home" ofType:@"png"]] forState:UIControlStateNormal];
[btnHome addTarget:self action:@selector(btnHomePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnHome];
UIButton *searchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame=CGRectMake(175, 2, 60, 40);
[searchBtn addTarget:self action:@selector(seachbtnPressed:) forControlEvents:UIControlEventTouchDown];
[searchBtn setImage:[UIImage imageNamed:@"Search.jpg"] forState:0];
[self.navigationController.navigationBar addSubview:searchBtn];
取三个UIButtons并添加到navigationBar.set帧图像为Per Ur Design。