【iOS】UI学习——导航控制器、分栏控制器
导航控制器https://img-blog.csdnimg.cn/direct/5a4ee4372ae14699bad68b7150f34728.png#pic_center
导航控制器负责控制导航栏(navigationBar),导航栏上的按钮叫UINavigationItem(导航元素项)。它还控制着一个视图控制器,即导航栏下面的东西。
导航控制器基础
#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//创建一个根视图控制器
VCRoot* root = [ init];
//创建导航控制器
//导航控制器主要用来管理多个视图控制器的切换
//层级的方式来管理多个视图控制器
//创建控制器时,一定要有一个根视图控制器
//P1:就是作为导航控制器的根视图控制器
UINavigationController* rev = [ initWithRootViewController:root];
//将window的根视图设置为导航控制器
self.window.rootViewController = rev;
;
}
新建一个VCRoot类
#import "VCRoot.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
;
//设置导航栏的透明度
//默认透明度为YES:可透明的
self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = ;
//设置导航栏的标题文字
self.title = @"娃哈哈";
//设置导航元素项目的标题
//如果没有设置元素项目的标题,系统会使用self.title作为标题;反之,优先为navigationItem.title
self.navigationItem.title = @"娃哈哈1";
//向左侧按钮中添加文字,这里是根据title文字来创建
//P1:栏按钮项的标题
//P2:按钮的样式
//P3:接受动作的目标对象
UIBarButtonItem* leftBtn = [ initWithTitle:@"旺仔牛奶" style:UIBarButtonItemStyleDone target:self action:@selector(pressLeft)];
self.navigationItem.leftBarButtonItem = leftBtn;
//右侧按钮中的文字是不可变的
//这里按钮是制定了系统提供的风格样式
//P1:按钮中展现的东西,注意,这里无论按钮中展现的是什么内容(无论图案或者文字),都是不可改变的
UIBarButtonItem* rightBtn = [ initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pressRight)];
//向右侧添加自定义按钮
UILabel* label = [ initWithFrame:CGRectMake(10, 10, 50, 40)];
label.text = @"矿泉水";
//将文字调至中间位置
label.textAlignment = NSTextAlignmentCenter;
label.textColor = ;
//UIView的子类都可以被添加
UIBarButtonItem* item = [ initWithCustomView:label];
//数组展现顺序从右至左
NSArray* array = ;
//将右侧按钮数组赋值
self.navigationItem.rightBarButtonItems = array;
//self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressLeft
{
NSLog(@"按下了左侧按钮");
}
-(void) pressRight
{
NSLog(@"按下了右侧按钮");
}
效果图:
https://img-blog.csdnimg.cn/direct/52dbe240546e4abbbadcbc0551c3638b.png#pic_center
导航控制器切换
navigationBar:导航栏对象
navigationItem:导航元素项对象
translucent:导航栏透明度
pushViewController:推入视图控制器
popViewController:推出视图控制器
首先创建三个视图
根视图VCRoot.m:
#import "VCRoot.h"
#import "VCTwo.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
self.view.backgroundColor = ;
//设置导航栏的透明度,默认为YES:可透明的;NO:不可透明的
self.navigationController.navigationBar.translucent = NO;
self.title = @"哦哦哦";
//设置导航栏的风格颜色,默认为Default
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
//为根视图的导航控制器设置右侧按钮
UIBarButtonItem* rightBtn = [ initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressRight
{
//创建新的视图控制器
VCTwo* vcTwo = [ init];
//使用当前视图控制器的导航控制器对象
;
}
第二个视图VCTwo.h:
#import "VCTwo.h"
#import "VCRoot.h"
#import "VCThree.h"
@interface VCTwo ()
@end
@implementation VCTwo
@synthesize elertView = _elertView;
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
//设置视图二的标题和颜色
self.view.backgroundColor = ;
UIBarButtonItem* leftBtn = [ initWithTitle:@"上一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressLeft)];
UIBarButtonItem* rightBtn = [ initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
self.navigationItem.leftBarButtonItem = leftBtn;
//;
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressLeft
{
//弹出当前视图控制器,返回上一个界面
;
}
-(void) pressRight
{
VCThree* vcThree = [ init];
//推入第三个视图控制器对象
;
}
第三个视图VCThree.h:
#import "VCThree.h"
#import "VCRoot.h"
#import "VCTwo.h"
@interface VCThree ()
@end
@implementation VCThree
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
self.view.backgroundColor = ;
UIBarButtonItem* leftBtn = [ initWithTitle:@"上一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressLeft)];
UIBarButtonItem* rightBtn = [ initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
self.navigationItem.leftBarButtonItem = leftBtn;
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressLeft
{
;
}
-(void) pressRight
{
//弹出当前视图,返回根视图
;
}
效果图:
https://img-blog.csdnimg.cn/direct/04b96bfc72c849ef89f944c6747d7a3b.png#pic_center
https://img-blog.csdnimg.cn/direct/457667465b6c4351b77f4a42ab9ff716.png#pic_center
https://img-blog.csdnimg.cn/direct/e07dc5a86b104afd9547f9d61bd43884.png#pic_center
导航栏和工具栏
ScenDelegate.m:
#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
VCRoot* vac = [ init];
UINavigationController* ans = [ initWithRootViewController:vac];
self.window.rootViewController = ans;
;
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
@end
VCRoot.h:
#import "VCRoot.h"
#import "VCSecond.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
self.view.backgroundColor = ;
self.title = @"根视图";
UIBarButtonItem* btn = [ initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = btn;
UINavigationBarAppearance* appearance = [ init];
//设置该对象的背景颜色
appearance.backgroundColor = ;
//创建该对象的阴影图像
appearance.shadowImage = [ init];
//设置该对象的阴影颜色
appearance.shadowColor = nil;
//设置导航栏按钮的颜色
self.navigationController.navigationBar.tintColor = ;
//设置普通样式导航栏
self.navigationController.navigationBar.standardAppearance = appearance;
//设置滚动样式导航栏
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
self.navigationController.navigationBar.hidden = NO;
self.navigationController.navigationBarHidden = NO;
//显示工具栏对象
//默认工具栏是隐藏的
self.navigationController.toolbarHidden = NO;
//设置工具栏是否透明
self.navigationController.toolbar.translucent = NO;
//向工具栏添加第一个按钮
UIBarButtonItem* btn1 = [ initWithTitle:@"left" style:UIBarButtonItemStylePlain target:nil action:nil];
//向工具栏添加第二个按钮
UIBarButtonItem* btn2 = [ initWithTitle:@"right" style:UIBarButtonItemStylePlain target:nil action:@selector(press)];
//添加一个自定义按钮
UIButton *btnC = ;
forState: UIControlStateNormal];
btnC.frame = CGRectMake(0, 0, 60, 60);
UIBarButtonItem *btn3 = [ initWithCustomView: btnC];
//设置一个占位按钮,放到数组中可以用来分隔开各按钮
//设置宽度固定按钮
UIBarButtonItem *btnF1 = [ initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target: nil action: nil];
btnF1.width = 110;
//设置自动计算宽度按钮
UIBarButtonItem *btnF2 = [ initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
//按钮数组的创建
NSArray *arrayBtn = ;
self.toolbarItems = arrayBtn;
}
效果图:
https://img-blog.csdnimg.cn/direct/09133566af2e489dabd8f1f12f3213bf.png#pic_center
分栏控制器
分栏控制器是管理多个视图控制器的管理控制器,通过数组的方式管理多个平行关系的视图控制器,与导航控制器的区别在于:导航控制器管理的是有层级关系的控制器。
注意:
分栏控制器在同一界面最多显示5个控制器切换按钮,超过5个时会自动创建一个新的导航控制器来管理其余的控制器。
分栏控制器基础
UITabBarItem:分栏按钮元素对象
badgeValue:分栏按钮提示信息
selectedIndex:分栏控制器选中的控制器索引
viewControllers:分栏控制器管理数组
selectedViewController:分栏控制器选中的控制器对象
VCone类
#import "VCone.h"
@interface VCone ()
@end
@implementation VCone
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
//创建一个分栏按钮对象
//P1:显示的文字
//P2:显示图片图标
//P3:设置按钮的tag
UITabBarItem* tab = [ initWithTitle:@"111" image:nil tag:101];
self.tabBarItem = tab;
}
@end
VCtow类:
#import "VCtwo.h"
@interface VCtwo ()
@end
@implementation VCtwo
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
//根据系统风格创建分栏按钮
//P1:系统风格设定
UITabBarItem* tab = [ initWithTabBarSystemItem:UITabBarSystemItemContacts tag:111];
tab.badgeValue = @"11";
self.tabBarItem = tab;
}
@end
VCthree类:
#import "VCthree.h"
@interface VCthree ()
@end
@implementation VCthree
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using .
// Pass the selected object to the new view controller.
}
*/
@end
SceneDelegate.m:
#import "SceneDelegate.h"
#import "VCone.h"
#import "VCtwo.h"
#import "VCthree.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//创建视图控制器1、2、3
VCone* vc1 = [ init];
vc1.title = @"视图一";
vc1.view.backgroundColor = ;
VCtwo* vc2 = [ init];
vc2.title = @"视图二";
vc2.view.backgroundColor = ;
VCthree* vc3 = [ init];
vc3.view.backgroundColor = ;
vc3.title = @"视图三";
//创建分栏控制器对象
UITabBarController* tbController = [ init];
//创建一个控制器数组对象
//将所有要被分栏控制器管理的对象添加到数组中去
NSArray* arrVC = ;
//给分栏控制器管理数组赋值
tbController.viewControllers = arrVC;
//将分栏控制器作为根视图控制器
self.window.rootViewController = tbController;
//设置选中的视图控制器的索引
tbController.selectedIndex = 2;
//当前显示的控制器对象
if(tbController.selectedViewController == vc3) {
NSLog(@"Right");
}
//是否分栏控制器的工具栏的透明度
tbController.tabBar.translucent = NO;
//分栏控制器的颜色
tbController.tabBar.backgroundColor = ;
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
@end
效果图:https://img-blog.csdnimg.cn/direct/9a2f839fc036452280bd3cccd787b955.png#pic_center
分栏控制器高级
willBeginCustomizingViewControllers:即将显示编辑方法
willEndCustomizingViewControllers:即将结束编辑方法
didEndCustomizingViewControllers:已经结束编辑方法
didSelectViewController:选中控制器切换方法
分栏控制器下面的导航栏最多显示5个按钮,超过5个按钮,体系会自动将末了一个按钮更换成more,当点击more时,才可以看到其他的按钮,点开后,右上角有一个Edit按钮,点击可以看到全部的按钮,也可拖动改变前四个按钮显现的是什么视图。
UITabBarControllerDelegate协议:
先创建VCone-Vcsix类,这里指显现VCone类:
#import "VCone.h"
@interface VCone ()
@end
@implementation VCone
- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using .
// Pass the selected object to the new view controller.
}
*/
@end
SceneDelegate.m:
#import "SceneDelegate.h"
#import "VCone.h"
#import "VCtwo.h"
#import "VCthree.h"
#import "VCfour.h"
#import "VCfive.h"
#import "VCsix.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
VCone* vc1 = [ init];
vc1.title = @"视图1";
vc1.view.backgroundColor = ;
VCtwo* vc2 = [ init];
vc2.title = @"视图2";
vc2.view.backgroundColor = ;
VCthree* vc3 = [ init];
vc3.title = @"视图3";
vc3.view.backgroundColor = ;
VCfour* vc4 = [ init];
vc4.title = @"视图4";
vc4.view.backgroundColor = ;
VCfive* vc5 = [ init];
vc5.title = @"视图5";
vc5.view.backgroundColor = ;
VCsix* vc6 = [ init];
vc6.title = @"视图6";
vc6.view.backgroundColor = ;
NSArray* arrVC = ;
UITabBarController* tb = [ init];
tb.viewControllers = arrVC;
tb.tabBar.translucent = NO;
tb.tabBar.backgroundColor = ;
self.window.rootViewController = tb;
//设置代理
//处理UITabBarControllerDelegate协议函数
tb.delegate = self;
}
//开始编译前调用此协议函数
-(void) tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{
NSLog(@"编辑前");
}
//即将结束编译前调用此协议函数
-(void) tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
NSLog(@"即将结束前");
}
//结束编译后调用此协议函数
-(void) tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
if(changed == YES) {
NSLog(@"顺序发生改变");
}
NSLog(@"已经结束编辑");
}
//选中控制器对象调用此协议函数
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"选中控制器对象");
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
@end
效果图:
https://img-blog.csdnimg.cn/direct/73dcdd0b895246ffb072c234174901c4.png#pic_center
点击more后:
https://img-blog.csdnimg.cn/direct/ad79fb8b8c8f479dbe443a023272106b.png#pic_center
点击Edit后:
https://img-blog.csdnimg.cn/direct/6689507e315d4aa7ade7e7f60fcdb5c1.png#pic_center
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]