【问题标题】:How to add Bar Button in navigation bar without navigation controller.如何在没有导航控制器的情况下在导航栏中添加栏按钮。
【发布时间】:2012-04-11 04:57:45
【问题描述】:

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

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

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

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

提前谢谢....

【问题讨论】:

    标签: ios ipad uinavigationbar


    【解决方案1】:

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

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

    我希望这对某人有帮助!

    【讨论】:

      【解决方案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];
      

      【讨论】:

        【解决方案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];
        

        【讨论】:

        • 在上面的代码中,这一行给了我错误。 navBar.rightBarButtonItem = btnSave; 错误:语义问题:在“UINavigationBar *”类型的对象上找不到属性“rightBarButtonItem”
        • 你也可以在xib文件中添加导航栏,然后你可以在其中添加bar button item。
        猜你喜欢
        • 1970-01-01
        • 2020-05-24
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多