以编程方式添加导航栏iOS [英] Adding Navigation Bar programmatically iOS

查看:103
本文介绍了以编程方式添加导航栏iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用导航栏(后退按钮,标题等)和标签栏(底部的工具栏)制作应用程序。我正在使用子视图,所以我不必担心状态栏,导航栏,标签栏高度等。但我认为这给我带来麻烦,因为我似乎无法弄清楚如何设置导航和标签栏。

I am trying to make an App with a Navigation Bar (Back button, title, etc) and a Tab Bar (Toolbar on the bottom). I am using subviews so I don't have to worry about the status bar, navigation bar, tab bar heights, etc. But I think it's causing me trouble because I can't seem to figure out how to setup the Nav and Tab Bars.

这就是我所拥有的。我做错了什么?

Here is what I have. What am I doing wrong?

AppDelegate.h

AppDelegate.h

(default for single view app)

AppDelegate.m

AppDelegate.m

(default for single view app)

ViewController.h

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) UIView *contentSubview;
@end

ViewController.m

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)loadView{}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor greenColor];
    self.contentSubview = [[UIView alloc] init];
    self.contentSubview.backgroundColor = [UIColor orangeColor];
    [view addSubview:self.contentSubview];
    self.view = view;
}

- (void)viewWillLayoutSubviews
    {
    [super viewWillLayoutSubviews];
    self.contentSubview.frame = CGRectMake(
                                       0,
                                       self.topLayoutGuide.length,
                                       CGRectGetWidth(self.view.frame),
                                       CGRectGetHeight(self.view.frame)-self.topLayoutGuide.length-self.bottomLayoutGuide.length
                                       );
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


推荐答案

-(void)ViewDidLoad
{
 UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];

UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@"karthik"];
// [navbar setBarTintColor:[UIColor lightGrayColor]];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

[navbar setItems:@[navItem]];
[self.view addSubview:navbar];
}


-(void)onTapDone:(UIBarButtonItem*)item{

}

-(void)onTapCancel:(UIBarButtonItem*)item{

}

Swift3

let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height:44)) // Offset by 20 pixels vertically to take the status bar into account

    navigationBar.backgroundColor = UIColor.white


    // Create a navigation item with a title
    let navigationItem = UINavigationItem()
    navigationItem.title = "Title"

    // Create left and right button for navigation item
     let leftButton =  UIBarButtonItem(title: "Save", style:   .plain, target: self, action: #selector(ViewController.btn_clicked(_:)))

    let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: nil)

    // Create two buttons for the navigation item
    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton

    // Assign the navigation item to the navigation bar
    navigationBar.items = [navigationItem]

    // Make the navigation bar a subview of the current view controller
    self.view.addSubview(navigationBar)




func btn_clicked(_ sender: UIBarButtonItem) {
    // Do something
}

Swift

 // Create the navigation bar
    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account

    navigationBar.backgroundColor = UIColor.whiteColor()
    navigationBar.delegate = self;

    // Create a navigation item with a title
    let navigationItem = UINavigationItem()
    navigationItem.title = "Title"

    // Create left and right button for navigation item
    let leftButton =  UIBarButtonItem(title: "Save", style:   UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
    let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

    // Create two buttons for the navigation item
    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton

    // Assign the navigation item to the navigation bar
    navigationBar.items = [navigationItem]

    // Make the navigation bar a subview of the current view controller
    self.view.addSubview(navigationBar)


  func btn_clicked(sender: UIBarButtonItem) {
  // Do something
 }

这篇关于以编程方式添加导航栏iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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