【发布时间】:2011-07-18 21:27:06
【问题描述】:
我在视图控制器中以编程方式创建了导航栏和标签。现在我想添加一个“完成”按钮,但似乎找不到不使用 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;
最后一部分显然不起作用...
感谢您的帮助!
【问题讨论】:
-
也许你读过这篇 [post][1] :) [1]: stackoverflow.com/questions/6464920/…
-
也许你读过这篇 [post][1] :) [1]: stackoverflow.com/questions/6464920/…
-
他正在使用 uinavigationcontoller,我没有。这是一个不同的情况。谢谢。
标签: iphone objective-c xcode uinavigationbar uinavigationitem