iOS NavigaitonController详解(code版)
参考文章:http://blog.csdn.net/totogo2010/article/details/7681879,参考了这篇文章,写的超级好,自己他的基础上加上了自己的理解。
下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出堆栈。
UINavigationController有Navigation bar ,Navigation View ,Navigation toobar等组成。
UINavigationController界面解析图片:
uinavigationController、uinavigationBar、uinavigationBarItem区分:
uinavigationController是个容器,里面可以装很多uiviewController。装这么多uiviewController让用户怎么控制它们呢,总得有个工具吧。这个工具就是uinavigationBar。一个容器就这么一个bar,相当于控制台吧。但是,管理那么多uiviewController,控制台上得按钮啊、标题啊,都千篇一律是不是看起来太无聊了。为了解决这个问题,uinavigationController为每个uiviewController生成一个uinavigationBarItem,通过这个uinavigationBarItem可以改变控制台“上面”得按钮和标题。
代码来实现导航视图界面的链接:
[self.navController pushViewController:rootView animated:YES]<pre name="code" class="objc">// 下面的代码是实现了一个全新的界面的创建。
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[testScrollViewViewControllerViewController alloc] initWithNibName:@"testScrollViewViewControllerViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible];
代码来实现导航视图界面的标题:
revigationBar.title = @”sakfjlsjf”; //添加标题 [revigationBar setTitle:@ ”sakfjlsjf”]; //另一种方法
代码来实现导航视图界面的UIBarButtonItem:
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddMethod)]; self.navigationItem.leftBarButtonItem = leftButton; //添加NavigationBar左右控件,初始化方法是定义一个按钮,然后将按钮和方法链接起来。
下图为UIBarButtonItem:系统自带的按钮风格:
用代码实现BarButton中间添加一个view:
NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil]; UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array]; [segmentedController addTarget:self action:nil forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView = segmentedController; UIButton *BarTitle = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [BarTitle setTitle:@"登陆界面" forState:UIControlStateNormal]; [BarTitle sizeToFit]; self.navigationItem.titleView = BarTitle;
代码实现自定义backBarButtonItem
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"返回登陆" style:UIBarButtonItemStyleDone target:self action:nil]; self.navigationItem.backBarButtonItem = button; //我们先创建一个UIBarButtonItem类,然后使用将他赋值给backBarButtonItem这个实例。
代码实现在ToolBar上添加UIBarButtonItem:
UIBarButtonItem *first = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil]; UIBarButtonItem *second = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil]; UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:nil]; UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray *toolBar = [NSArray arrayWithObjects:flexItem, first, flexItem, second, flexItem, three, flexItem, four, flexItem, nil]; self.toolbarItems = toolBar;
用代码实现自定义 navigationbar 的左右按钮
// 自定义按钮 UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [actionBtn setTitle:@"" forState:UIControlStateNormal]; UIImage *imgDefault = [UIImage imageNamed:@"Arrow"]; [actionBtn setFrame:CGRectMake(0, 0, imgDefault.size.width, imgDefault.size.height)]; [actionBtn setBackgroundImage:imgDefault forState:UIControlStateNormal]; [actionBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:actionBtn];
用代码实现自定义navigationbar的title标题
自定义标题 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.font = [UIFont systemFontOfSize:17]; titleLabel.textColor = [UIColor colorWithRed:0 green:0.44 blue:0.70 alpha:1 ];//设置文本颜色 titleLabel.textAlignment = UITextAlignmentCenter; titleLabel.text = @"Resert Password"; self.navigationItem.titleView = titleLabel;
用代码实现改变navigationbar的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
在navigation的编写的过程中,如果我们将navigation的navigationBar设置为透明,那么视图在xib中的位置不会改变;反之,视图中的xib中的位置将会整体下移64个单位。下面是改透明度的代码
// 在这里将导航栏设置为透明 self.navigationController.navigationBar.translucent = YES;