ios - 以编程方式添加导航栏 iOS

标签 ios objective-c iphone swift xcode

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

这是我的。我做错了什么?

AppDelegate.h

(default for single view app)

AppDelegate.m

(default for single view app)

ViewController.h

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

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 - 以编程方式添加导航栏 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448766/

相关文章:

iOS - 将时间和日期转换为用户时区

ios - UITableView deleteRows 未更新 IndexPaths 导致崩溃索引超出范围

ios - 呈现和关闭最后一个 UIViewController

ios - 动态收缩 SKSpriteNode

ios - 如何在某些情况下停止 iPad 旋转?

ios - 在 Sketch 3 中导出所有 iPhone

iphone - 我可以使用 PAN over Bluetooth 将配件连接到 iPad 吗?

ios - 时间戳 413033364.14671 是什么格式?

ios - 如何将自定义值插入现有的 Plist 数组数据?

iphone - iPhone 应用程序中媒体文件的存储位置