如何添加栏按钮在导航栏上没有导航控制器。(How to add Bar Button in navi

2019-06-23 12:37发布

我是新来的iOS development.I在我的iPad应用view.I不需要导航控制器,这就是为什么我仅添加了导航栏创建一个导航栏。 现在我想在导航bar.I尝试了很多,但没有成功添加按钮。 是否有可能只添加导航栏与按钮? 如果是的话那么建议我一些示例代码。

“我没有导航控制器或需要它。我只想补充导航栏只有一个视图。”

贝娄是我的代码,我写的viewDidLoad中添加导航栏()

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

提前致谢....

Answer 1:

我用Interface Builder来使静态的tableView在一个UITableViewController。 此的UITableViewController有模式地示出。 然后,我添加了一个导航栏没有UINavigationController的背后如下:

//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];

我希望这可以帮助别人!



Answer 2:

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButton)];

bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];

self.navigationItem.rightBarButtonItem = bi1;

[bi1 release];


Answer 3:

您可以添加按钮,导航栏,如下所示:

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Save"
                                style:UIBarButtonItemStyleBordered 
                                target:self 
                             action:@selector(save_Clicked:)];
 navBar.rightBarButtonItem = btnSave;
 [btnSave release];

 UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Cancel"                                    
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(cancel_Clicked:)];
 navBar.leftBarButtonItem = btnCancel;
 [btnCancel release];


文章来源: How to add Bar Button in navigation bar without navigation controller.