我可以添加一个完全免费的 UIBarButtonItem 吗?

发布于 2024-11-25 01:48:59 字数 1147 浏览 0 评论 0原文

我在视图控制器中以编程方式创建了一个导航栏和一个标签。现在我想添加一个“完成”按钮,但似乎无法找到不使用 IB 的方法......有什么方法可以做到这一点吗?

这是我的视图控制器的 viewDidLoad:

//Adding navBar programmatically 
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];

//Adding label to navBar programmatically 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,width-20,14)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = @"TITLE";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:22];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];

//Adding back button to navBar programmatically 
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)];
self.navigationItem.leftBarButtonItem = rightButton;   

最后一部分显然不起作用...

感谢您的帮助!

I created a nav bar and a label programmatically in my view controller. Now I want to add a "done" button but cant seem to find a way without using IB....is there any way to do that?

Here is my view controller's viewDidLoad:

//Adding navBar programmatically 
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];

//Adding label to navBar programmatically 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,width-20,14)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = @"TITLE";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:22];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];

//Adding back button to navBar programmatically 
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)];
self.navigationItem.leftBarButtonItem = rightButton;   

Last part obviously doesnt work...

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-12-02 01:48:59

为什么不使用 UIToolBar 来代替?

要将按钮添加到 UINavigationBar,您需要创建一个 UINavigationItem。如果您将 UINavigationBar 用作 UINavigationController 的一部分(这是常见情况),则这是自动完成的,作为独立视图,您必须自己执行此操作。

UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:@""] autorelease];
item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissView:)] autorelease];
[navBar setItems:[NSArray arrayWithObject:item] animated:NO];

Why are you not using a UIToolBar instead?

To add buttons to a UINavigationBar, you need to create a UINavigationItem. If you're using UINavigationBar as part of UINavigationController (as is the common case), this is done automatically, as a standalone view, you have to do this yourself.

UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:@""] autorelease];
item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissView:)] autorelease];
[navBar setItems:[NSArray arrayWithObject:item] animated:NO];
若能看破又如何 2024-12-02 01:48:59

仅当您在导航控制器中推送视图控制器时才使用 navigationItem,

在您的情况下,您需要

使用-pushNavigationItem:animated:

在 UINavigationBar 上

检查文档以获取更多详细信息:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html

The navigationItem is only used when you push a viewcontroller in a navigationcontroller

in your case you need to use

-pushNavigationItem:animated:

on the UINavigationBar

Check the docs for more details:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文