如何在没有导航控制器的导航栏中添加条形按钮。 [英] How to add Bar Button in navigation bar without navigation controller.

查看:82
本文介绍了如何在没有导航控制器的导航栏中添加条形按钮。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手。我在iPad应用程序视图中创建了一个导航栏。我不需要导航控制器,这就是为什么我只添加了导航栏。现在我想在导航栏中添加按钮。我尝试了很多但没有成功。是否可以仅添加带按钮的导航栏?如果是,那么建议我一些示例代码。

I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.

我没有导航控制器或需要它。我只想在一个视图中添加导航栏。

"i don't have navigation controller or need it. i just want to add navigation bar in only one view."

Bellow是我为在ViewDidLoad()中添加导航栏而编写的代码

Bellow is my code which i write for adding navigation bar in ViewDidLoad()

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];

提前致谢....

推荐答案

我使用Interface Builder在UITableViewController中创建一个静态tableView。
这个UITableViewController以模态显示。然后我添加了一个没有UINavigationController的NavigationBar,如下所示:

I used the Interface Builder to make a static tableView in a UITableViewController. This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:

//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:@"Sign-In"];

//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Zurück" style:UIBarButtonItemStyleDone target:self action:@selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStylePlain target:self action:@selector(signInDonePressed:)];

//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];

//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];

// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];

// Adding the NavigationBar to the TableView 
[self.tableView setTableHeaderView:headerView];

我希望这有助于某人!

这篇关于如何在没有导航控制器的导航栏中添加条形按钮。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆