以编程方式将按钮添加到导航栏
您好,我需要以编程方式在导航栏中的右侧设置按钮,这样如果我按下按钮,我将执行一些操作。我已经以编程方式创建了导航栏;
navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
同样,我需要在该导航栏的右侧添加按钮。对于我使用的,
1。
UIView* container = [[UIView alloc] init];
// create a button and add it to the container
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)];
[container addSubview:button];
[button release];
// add another button
button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)];
[container addSubview:button];
[button release];
// now create a Bar button item
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
[item release];
2.
UIImage *im;
im=[UIImage imageNamed:@"back.png"];
[button setImage:im forState:UIControlStateNormal];
[im release];
backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)];
[self.navigationItem setRightBarButtonItem:backButton];
3.
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];
self.navigationItem.rightBarButtonItem = refreshItem;
[refreshItem release];
我尝试了所有这些方法,但没有一个在右侧显示按钮。
Hi I need to set the button on right side, in navigation bar, programatically , so that if I press the button I will perform some actions. I have created the navigation bar, programatically by;
navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
Similarly, I need to add the button, on the right side of this navigation bar. For that I used,
1.
UIView* container = [[UIView alloc] init];
// create a button and add it to the container
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)];
[container addSubview:button];
[button release];
// add another button
button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)];
[container addSubview:button];
[button release];
// now create a Bar button item
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
[item release];
2.
UIImage *im;
im=[UIImage imageNamed:@"back.png"];
[button setImage:im forState:UIControlStateNormal];
[im release];
backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)];
[self.navigationItem setRightBarButtonItem:backButton];
3.
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];
self.navigationItem.rightBarButtonItem = refreshItem;
[refreshItem release];
I tried all these ways, but none is displaying the button on the right hand side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在我的
UIViewController
派生类中,我在viewDidLoad
中使用以下内容:这将在右侧添加一个标题为 Flip 的按钮,该按钮调用以下方法:
-(IBAction)flipView
这看起来很像你的#3,但它在我的代码中工作。
Inside my
UIViewController
derived class, I am using the following insideviewDidLoad
:This adds a button to the right hand side with the title Flip, which calls the method:
-(IBAction)flipView
This looks very much like you #3, but it is working within my code.
///// 调用事件
SWIFT:
///// called event
SWIFT:
要在导航栏上添加搜索按钮,请使用此代码:
并实现以下方法:
To add search button on navigation bar use this code:
and implement following method:
如何在 swift:
onAdd 中向导航栏添加添加按钮:
How to add an add button to the navbar in swift:
onAdd:
Swift 版本,将其添加到 viewDidLoad 中:
并将其添加到您的 View Controller 类中
Swift version, add this in viewDidLoad:
And this in your View Controller class
使用以下代码:
Use following code:
像这样简单地使用本机 editBarButton
然后
玩得开心......!!!
Simple use native editBarButton like this
and then
Have fun...!!!
在ViewController.m的ViewDidLoad方法中
“(back)” 选择器是一个关闭当前ViewController的方法
In ViewDidLoad method of ViewController.m
"(back)" selector is a method to dissmiss the current ViewController
试试这个,对我有用。
以编程方式将按钮添加到导航栏,我们还将图像设置为导航栏按钮,
下面是代码:-
Try this.It work for me.
Add button to navigation bar programmatically, Also we set image to navigation bar button,
Below is Code:-
如果您不是在寻找 BarButtonItem 而是在导航栏上寻找简单的按钮,那么下面的代码可以工作:
If you are not looking for a BarButtonItem but simple button on navigationBar then below code works:
大家好!!我创建了针对当前问题的解决方案,其中使用 UIIMagePicker 需要两个 UIInterface 方向。在我的 ViewController 中,我处理 UIImagePickerController 的转场
**我使用 a..
**
然后在 AppDelegate 类中执行以下操作。
Hello everyone !! I created the solution to the issue at hand where Two UIInterface orientations are wanted using the UIIMagePicker.. In my ViewController where I handle the segue to the UIImagePickerController
**I use a..
**
Then in the AppDelegate Class I do the following.