UINavigationController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//导航视图控制器也是一个视图控制器,TA管理了多个子视图控制器,是系统提供给我们的容器视图控制器。
//导航视图控制器至少管理一个子视图控制器,这个视图控制器称为导航视图控制器的根视图控制器
//如果我们的程序想要采用导航视图控制器进行布局,我们需要指定window的根视图控制器为导航视图控制器
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
//设置导航栏的显隐属性
naVC.navigationBarHidden = NO;
self.window.rootViewController = naVC;
//设置导航栏样式
naVC.navigationBar.barStyle = UIBarStyleDefault;
//设置导航条的背景颜色
naVC.navigationBar.backgroundColor = [UIColor greenColor];
//设置导航栏颜色
naVC.navigationBar.barTintColor = [UIColor greenColor];
//设置导航栏元素颜色
naVC.navigationBar.tintColor = [UIColor yellowColor];
[rootVC release];
[naVC release];
[self.window makeKeyAndVisible];
return YES;
}
@implementation RootViewController
- (void)leftAction {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//每一个加到导航视图控制器内部的视图控制器自带一个属性叫navigationItem,可以配置当前页面导航条的显示内容,比如左、右按钮,标题等
self.navigationItem.title = @"中二洪荒";
//创建左按钮
/* 1 显示标题
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"巴达" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];*/
/* 2 使用系统自带图标样式
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(leftAction)];*/
/* 3 使用自定义图片显示
UIImage *image = [UIImage imageNamed:@"dianhua"];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
//指定左按钮
self.navigationItem.leftBarButtonItem = left;*/
// 4 使用自定义视图显示
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.view addSubview:mySwitch];
//
// UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];
// self.navigationItem.leftBarButtonItem = left;
//
// [mySwitch release];
// [left release];
//设置左、右按钮显示多个
UIBarButtonItem *first = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
UIBarButtonItem *second = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(bookmark)];
self.navigationItem.rightBarButtonItems = @[first, second];
[first release];
[second release];
//设置导航条上的标题视图
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"中二洪荒", @"洪荒中二"]];
seg.selectedSegmentIndex = 0;
// seg.momentary = YES;
self.navigationItem.titleView = seg;
[seg release];
// 导航栏半透明效果(iOS7以后 默认为YES)
// 当半透明效果开启时 屏幕左上角为坐标原点
// 关闭时 导航栏左下角为坐标原点
self.navigationController.navigationBar.translucent = NO;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(125, 100, 100, 100)];
view.tag = 101;
view.backgroundColor = [UIColor blackColor];
[self.view addSubview:view];
[view release];
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 300, 100, 40)];
stepper.minimumValue = 1;
stepper.maximumValue = 5;
stepper.stepValue = 1;
[stepper addTarget:self action:@selector(step:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:stepper];
[stepper release];
// Do any additional setup after loading the view.
}
- (void)step:(UIStepper *)stepper {
UIView *view = [self.view viewWithTag:101];
// UIAlertView
// UIActionSheet
// UIAlertController
// UIDatePicker
// UIPickerView
view.bounds = CGRectMake(0, 0, 100 + 50 * stepper.value, 100 + 50 * stepper.value);
}
- (void)add {
NSLog(@"左边添加");
}
- (void)bookmark {
NSLog(@"右边添加");
}