导航控制器的学习

来源:互联网 发布:云计算平台storm 编辑:程序博客网 时间:2024/05/22 01:43

import “ViewController.h”

import “AViewController.h”

@interface ViewController ()
{
UIView *view;
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

// self.title = @”Root”;

/*
UINavigationController
导航控制器层次结构

继承于 UIViewController

ToolBar 工具栏

ContentView 是加载在 其根视图上

NaviagationBar 导航栏

导航控制器的作用?
它采用栈的原理来管理视图控制器
*/
//导航控制器显示的View为当前栈顶视图控制器的View
self.view.backgroundColor = [UIColor redColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];button.translatesAutoresizingMaskIntoConstraints = NO;[button setTitle:@"push" forState:0];[button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:button];NSLayoutConstraint *constraint; constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];[self.view addConstraint:constraint];constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];[self.view addConstraint:constraint];constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.2 constant:0];[self.view addConstraint:constraint];constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44];[self.view addConstraint:constraint];//默认导航控制器是隐藏的

// self.navigationController.toolbarHidden = NO;

/————–关于导航栏——————–/

// self.navigationController.navigationBar
//设置背景,现在开发过程中基本不会设置背景图片,而是设置背景颜色
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@”1”] forBarMetrics:UIBarMetricsDefault];

// [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];

//导航栏的中间有一个titleViewUIButton *titleButton = [UIButton buttonWithType:UIButtonTypeSystem];[titleButton setTitle:@"点我" forState:0];titleButton.frame = CGRectMake(0, 0, 60, 30);[titleButton addTarget:self action:@selector(clickmeAction:) forControlEvents:UIControlEventTouchUpInside];self.navigationItem.titleView = titleButton;view = [[UIView alloc] initWithFrame:CGRectMake(10, 24, 100, 100)];view.backgroundColor = [UIColor yellowColor];////    [self.view insertSubview:view atIndex:0];

// view.alpha = 0.0;//透明度
view.hidden = YES;

UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];[window addSubview:view];//导航栏的左边存在UIBarButtonItemUIBarButtonItem *barButtonItem1;

// barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@”返回” style:UIBarButtonItemStylePlain target:nil action:nil];
barButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
UIBarButtonItem * barButtonItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
UIBarButtonItem * barButtonItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil];
// self.navigationItem.leftBarButtonItem = barButtonItem;

//设置item的间距/* UIBarButtonSystemItemFlexibleSpace灵活的 UIBarButtonSystemItemFixedSpace固定的 */UIBarButtonItem *spaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];//间距为60

// spaceBarButtonItem.width = 60;

self.navigationItem.leftBarButtonItems = @[barButtonItem1,spaceBarButtonItem,barButtonItem2];

// UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 100, 320, 44)];
// toolBar.items = @[barButtonItem1,spaceBarButtonItem,barButtonItem2,spaceBarButtonItem,barButtonItem3];
// [self.view addSubview:toolBar];
// UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(0, 140, 320, 30)];
// text.placeholder = @”placeholder”;
// text.inputAccessoryView = toolBar;
// [self.view addSubview:text];

//导航栏的toolBarself.navigationController.toolbarHidden = NO;self.toolbarItems = @[barButtonItem1,spaceBarButtonItem,barButtonItem2,spaceBarButtonItem,barButtonItem3];

}

/*
每个视图控制器都具有模态视图推送功能,即此方法
[self presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
当视图控制器被压入导航控制器中,就具备navigationController属性,就具有此方法[self.navigationController pushViewController:aViewController animated:YES];
*/

-(void)pushAction:(UIButton *)sender{

AViewController *aViewController = [[AViewController alloc] init];[self.navigationController pushViewController:aViewController animated:YES];

}

-(void)clickmeAction:(UIButton *)sender{
view.hidden = !view.hidden;

}

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

@end
子类AviewController的代码

import “AViewController.h”

import “BViewController.h”

@interface AViewController ()

@end

@implementation AViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @”AVC”;

    self.view.backgroundColor = [UIColor cyanColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.translatesAutoresizingMaskIntoConstraints = NO;
    [button setTitle:@”push” forState:0];
    [button addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    NSLayoutConstraint *constraint;
    constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.2 constant:0];
    [self.view addConstraint:constraint];

    constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44];
    [self.view addConstraint:constraint];

}

-(void)popAction:(UIButton *)sender{

[self.navigationController pushViewController:[BViewController new] animated:YES];
0 0
原创粉丝点击