zoukankan      html  css  js  c++  java
  • iOS:导航栏的工具条和导航条

    功能:用NAV视图控制器打开新的视图,默认工具条和导航条隐藏,双击显示之

    //
    //  main.m
    //  Hello
    //
    //  Created by lishujun on 14-8-28.
    //  Copyright (c) 2014年 lishujun. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface TestViewController : UIViewController
    @end
    
    @implementation TestViewController
    -(void) loadView
    {
        NSLog(@"load View");
        //创建视图对象
        UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        contentView.backgroundColor = [UIColor lightGrayColor];
        self.view = contentView;
    }
    @end
    
    // --------------视图控制器对象--------------
    @interface HelloWorldViewController : UIViewController
    {
        UIToolbar *toolBar;
    }
    @end
    
    @implementation HelloWorldViewController
    
    -(void) loadView
    {
        NSLog(@"load View");
        //创建视图对象
        UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        contentView.backgroundColor = [UIColor lightGrayColor];
        self.view = contentView;
        
        //设置工具栏并显示
        [self setNavBarAndShow];
        [self setUserToolBarAndShow];
    }
    
    //-----------设置导航栏并显示-----------------
    -(void) setNavBarAndShow
    {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(pushView:)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Prev" style:UIBarButtonItemStylePlain target:self action:@selector(popView:)];
        [self.navigationController setNavigationBarHidden:NO]; // 显示之前隐藏的导航栏
    }
    
    -(void) pushView:(id)sender
    {
        NSLog(@"push view");
        [self.navigationController pushViewController:[[TestViewController alloc]init] animated:YES];
    }
    
    -(void) popView:(id)sender
    {
        NSLog(@"pop view");
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    //------------设置工具栏并显示--------------
    -(void) setNavToolBarAndShow
    {
        // 设置NavigationController上的工具栏并显示
        UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
        UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
        UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
        UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];
        
        [self.navigationController setToolbarHidden:NO animated:NO];
    }
    
    -(void) setUserToolBarAndShow
    {
        //设置自定义的toolbal
        [self.navigationController  setToolbarHidden:YES animated:YES];
        
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
        toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];
        [toolBar setBarStyle:UIBarStyleDefault];
        toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        [toolBar setItems:[NSArray arrayWithObject:addButton]];
        [self.view addSubview:toolBar];
    }
    
    @end
    
    
    // ----------------委托对象--------------------
    @interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>
    {
        IBOutlet UIWindow *window;
    }
    
    @property (nonatomic, retain) UIWindow *window;
    @property (nonatomic, retain) UINavigationController *nav;
    
    @end
    
    @implementation HelloWorldAppDelegate
    
    @synthesize window;
    @synthesize nav;
    
    -(void) applicationDidFinishLaunching:(UIApplication *)application
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
        HelloWorldViewController *viewController = [[HelloWorldViewController alloc]init];
        
        self.nav = [[UINavigationController alloc]initWithRootViewController: viewController];
        [self.nav setNavigationBarHidden:YES];        //隐藏导航栏,位于视图顶部
        [self.nav setToolbarHidden:YES];              //隐藏工具栏,位于视图底部
        
        self.window.rootViewController = self.nav;
        [self.window makeKeyAndVisible];
    }
    @end
    
    // ---------------程序入口---------------------
    int main(int argc, char * argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
        }
    }
  • 相关阅读:
    C#Redis缓存帮助类
    [RxSwift教程]9、过滤操作符:filter、take、skip等
    [RxSwift教程]8、变换操作符:buffer、map、flatMap、scan等
    [RxSwift教程]7、Subjects、Variables
    [RxSwift教程]6、观察者2: 自定义可绑定属性
    [RxSwift教程]5、观察者1: AnyObserver、Binder
    [RxSwift教程]4、Observable订阅、事件监听、订阅销毁
    [RxSwift教程]3、Observable介绍、创建可观察序列
    [RxSwift教程]2、响应式编程与传统式编程的比较样例
    [RxSwift教程]1、安装、介绍
  • 原文地址:https://www.cnblogs.com/code-style/p/3952174.html
Copyright © 2011-2022 走看看