xcode 如何以编程方式在 iPhone 的 UINavigationbar 中创建三个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7631305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create three buttons in UINavigationbar in iPhone programmatically?
提问by ravikumar karunanithi
I'm developing a small application. I need to create three buttons in a subclass. One button is add
, another one is search
and the last one is back
. I also create left and right buttons. But I can't create search button in the center of the navigation bar. How can I create it? My code is:
我正在开发一个小应用程序。我需要在一个子类中创建三个按钮。一个按钮是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];
}
How to create a middle button in the navigation bar? Please help me.
如何在导航栏中创建中间按钮?请帮我。
回答by NIKHIL
Below is the code to use segment control in navigation bar programatically
下面是在导航栏中以编程方式使用段控件的代码
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];
回答by Srinivas
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];
take three UIButtons and Add to navigationBar.set Frames Images As Per Ur Design.
取三个 UIButtons 并添加到 navigationBar.set Frames Images As Per Ur Design。