2014-01-30 58 views
24

我正在嘗試使用導航欄(後退按鈕,標題等)和選項卡欄(底部的工具欄)製作應用程序。我正在使用子視圖,所以我不必擔心狀態欄,導航欄,標籤欄高度等。但我認爲這給我造成麻煩,因爲我似乎無法弄清楚如何設置導航欄和製表欄。以編程方式添加導航欄iOS

這是我的。我究竟做錯了什麼?

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 
+0

是什麼此代碼的任何有加一個導航欄或工具欄呢?你想添加一個導航欄和工具欄,還是你想要一個導航控制器?你想要完成什麼? – rdelmar

+0

我想這就是我困惑的地方。我想添加一個導航欄和工具欄。但我正在使用子視圖,所以我不確定是否必須做一些不同的事情。 – boxmatic

回答

66
-(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 
} 

夫特

// 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 
} 
+1

我得到這個工作,並顯示一個導航欄。然而,確定標題和項目已被證明是困難的。 – boxmatic

+1

我也想設置一個帶有圖標的標籤欄。 – boxmatic

+0

@boxmatic你是如何設置標題和項目的? – SonOfSeuss

4

XIB方法:

教程從here

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Create the PlainViewController (and give it a title) 
    PlainViewController *plainView = [[PlainViewController alloc] initWithNibName:@"PlainViewController" bundle:nil]; 
    [plainView setTitle:@"PlainView"]; 

    // Create the NavRootView controller (and give it a title) 
    NavRootView *navRoot = [[NavRootView alloc] initWithNibName:@"NavRootView" bundle:nil]; 
    [navRoot setTitle:@"NavRoot"]; 

    // Create our navigation controller using our NavRootView as it's root view 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:navRoot]; 

    // Make an array containing our plain view controller and our navigation controller 
    NSArray *viewArray = [NSArray arrayWithObjects:plainView, navController, nil]; 

    // Release the views and nav controller 
    [plainView release]; 
    [navRoot release]; 
    [navController release]; 

    // Create our tab bar controller 
    UITabBarController *tabbarController = [[UITabBarController alloc] init]; 

    // Tell the tab bar controller to use our array of views 
    [tabbarController setViewControllers:viewArray]; 

    // Finally, add the tabbar controller as a subview of the app window 
    [window addSubview:[tabbarController view]]; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

唯一代碼:

howto-implement-uinavigationcontroller-uitabbarcontroller-programmatically

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    /* Initialize window view */ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    /* Initialize tab bar controller, add tabs controllers */ 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [self initializeTabBarItems]; 
    self.window.rootViewController = self.tabBarController; 

    [self.window makeKeyAndVisible]; 
    return (YES); 
} 

- (NSArray *)initializeTabBarItems 
{ 
    NSArray * retval; 

    /* Initialize view controllers */ 
    ViewController1 * viewController1 = [[ViewController1 alloc] init]; 
    ViewController2 * viewController2 = [[ViewController2 alloc] init]; 
    ViewController3 * viewController3 = [[ViewController3 alloc] init]; 
    ViewController4 * viewController4 = [[ViewController4 alloc] init]; 
    ViewController5 * viewController5 = [[ViewController5 alloc] init]; 

    /* Initialize navigation controllers */ 
    UINavigationController * navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
    UINavigationController * navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
    UINavigationController * navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3]; 
    UINavigationController * navigationController4 = [[UINavigationController alloc] initWithRootViewController:viewController4]; 
    UINavigationController * navigationController5 = [[UINavigationController alloc] initWithRootViewController:viewController5]; 



    /* Stuff Navigation Controllers into return value */ 
    retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,viewController4,viewController5,nil]; 



    return (retval); 
} 

故事板:

storyboards-tutorial-in-ios-7

enter image description here

+0

本教程有點舊了。我試圖使用唯一的代碼版本,我一直無法使它工作。 – boxmatic

-2

斯威夫特版本,在viewDidLoad中添加此:

var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneButton:") 
navigationItem.rightBarButtonItem = doneButton 

這在您的視圖控制器類

func doneButton(sender: UIBarButtonItem) { 
    println(111) 
} 
+4

問題是關於添加一個導航欄,而不是如何爲酒吧按鈕項添加處理程序 – genaks

相關問題