ViewController跳转分界面包车型客车二种艺术差十分少计算,教您十分钟搭建贰个很好的tabbar形式的花色
小说为我劳顿码出来的,转发请证明出处,喜欢的话请star一下。示例代码链接:

3、通过导航调控器UINavigationController

  • 导航调控器
    :绝对是最常用的跳转方法,也是豪门最熟谙的一种艺术。每种调控器对象都有七个NavigationController属性,NavigationController的view的是由导航条,导航条调整的view,和栈顶调整器的view组成的。
  • 专门的职业原理:通过栈的方法的来兑现的,NavigationController显示永久就是栈顶的控制器的view。当使用push方法的时候,就将索要跳转的调整器压入栈中,成为栈顶调整器;当使用pop方法的时候,就将调整器移出栈,原本跳转在此以前的调控珍视新产生栈顶调节器,被表现;
  • 亟需注意的是:跳转的时候,跳转前的调控器不会移除;导航栏(UINavigationBar)的特性由栈顶调节器来调节。UINavigationBar帮忙appearance统一设置,但UINavigationItem不帮忙;
  • 事关到的类详解:
    • UINavigationBar
      :继承至UIView,NavigaitonBar便是导航栏,位于显示屏的最上部,管理整个NavigationController的navigationItem,即类似navigationcontroller一样提供了一个栈来处理item。
    • UINavigationItem : 承袭至NSObject,通过那性子情来安装title
      ,prompt,leftBarButtonItem,titleView,,rightBarButtonItem,backBarButonItem
      等。
    • UIBarButtonItem
      :承袭至UIBarItem,UIBarItem承接至UIButton。特地用来放在UIToolbar
      只怕 UINavigationBar的特出button。

小结:NavigationController直接调控ViewControllers,并带有NavigaitonBar。NavigaitonBar包罗全部UINavigationItem的栈,管理整个NavigationController的UINavigationItem(
NSArray *items 属性)。
UINavigationItem包涵了NavigaitonBar视图的一体成分(如title,tileview,backBarButtonItem等),又受当前栈顶调整器管理,即NavigaitonBar形成全方位NavigationController的领航视图,然后各种NavigationController页面包车型客车导航栏成分由所在页面包车型客车UINavigationItem管理。即设置当前页面包车型地铁左右barbutton。

先来看一下效果图:

1.支撑导航栏颜色自定义2.援救回到按键自定义3.支撑导航栏右边按键自定义4.支撑导航栏标题自定义5.匡助具有页面手势侧滑6.帮忙tabbar
item 文字颜色自定义(选中状态和非选中状态)

图片 1

  • 1.导入原能源,如tabbar的items图片等
  • 2.创建.pch文件

在档案的次序->Bulid Settings->搜prefix
header->在后头加入你的.pch文件的相对路线(贰个简易方法正是点出prefix
header前边的框后,把工程目录里面包车型大巴.pch文件拖到框里,就自动生成它的相对路线了)

  • 3.导入要求运用的三方库MLNavigationController,这几个调控器承继自UINavigationController,参预了归来手势也可以用系统的导航栏,系统的导航栏现在也自带返回滑动手势
  • 4.创建承接于UITabBarController的tabbar控制器
  • 5.AppDelegate中初步化

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[SJTabBarViewController alloc] init];;[self.window makeKeyAndVisible];
  • 6.创制基类调控器
    如:SJBaseViewController。在基类调节器里面首要安装导航栏及其一些属性~6.1
    基类 .h文件

    图片 2

    ~6.2 基类 .m文件//设置导航栏颜色

 - setNavBarStyle{ self.titleViewLB.textColor = color_whiteColor; self.navigationController.navigationBar.barTintColor = kNavigationBarColor; self.navigationController.navigationBar.translucent = NO; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; UINavigationBar *navigationBar = self.navigationController.navigationBar; [navigationBar setBackgroundImage:[UIImage imageNamed:@"account_headBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; [navigationBar setShadowImage:[UIImage new]];}

//baseBack方法

 - baseBack{ [self.navigationController popViewControllerAnimated:YES];}

//setter and getter方法

 - (UILabel *)titleViewLB{ if (_titleViewLB == nil) { _titleViewLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth-140, 44)]; _titleViewLB.backgroundColor = [UIColor clearColor]; _titleViewLB.textColor = color_whiteColor _titleViewLB.font = XSBlodFont; _titleViewLB.textAlignment = NSTextAlignmentCenter; } return _titleViewLB;} - setNavTitle:(NSString *)navTitle{ _titleViewLB.text = navTitle;} - setNavTitleColor:(UIColor *)navTitleColor{ _titleViewLB.textColor = navTitleColor;} - setShowBack:showBack{ if  { _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; _backButton.frame = CGRectMake(0, 0, 20, 44); [_backButton setImage:[UIImage imageNamed:@"back-white"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector forControlEvents:UIControlEventTouchUpInside]; _backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10); UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:_backButton]; backItem.style = UIBarButtonItemStylePlain; self.navigationItem.leftBarButtonItem = backItem; }else{ self.navigationItem.hidesBackButton = YES; } //解决titleView不能居中的问题 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 35, 35); button.hidden = YES; UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.rightBarButtonItem = right;} - setShowLeftButton:showLeftButton{ if (showLeftButton) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 35, 35); self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; [button addTarget:self action:@selector(leftBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside]; }} - setShowRightButton:showRightButton{ if (showRightButton) { _rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; _rightButton.frame = CGRectMake(0, 0, 30, 30); _rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15); //可以根据自己想要的图标在目标控制器里设置// [_rightButton setImage:[UIImage imageNamed:@"more_icon_forum"] forState:UIControlStateNormal]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton]; [_rightButton addTarget:self action:@selector(rightBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside]; }}

//touch up inside响应开关方法

 - leftBarButtonItemTouchedUpInSide{ //根据自己需要实现 NSLog(@"leftBarButtonItemTouchedUpInSide未实现");} - rightBarButtonItemTouchedUpInSide{ //根据自己需要实现 NSLog(@"rightBarButtonItemTouchedUpInSide未实现");}
  • 7.创办tabbar Items
    调整器,tabbar有多少个items就创办多少个,承继自BaseViewController

  • 8.创建tabbar控制器,继承自UITabBarController~8.1
    服从契约:UITabBarDelegate,UITabBarControllerDelegate~8.2
    items调节器组

    @property(nonatomic,strong)NSMutableArray *viewControllers;

~8.3 伊始化调控器

 - setUpControllers{ SJOneViewController * oneVC = [[SJOneViewController alloc] init]; MLNavigationController *oneNav = [[MLNavigationController alloc] initWithRootViewController:oneVC]; SJTwoViewController * twoVC = [[SJTwoViewController alloc] init]; MLNavigationController *twoNav = [[MLNavigationController alloc] initWithRootViewController:twoVC]; SJThreeViewController *threeVC = [[SJThreeViewController alloc] init]; MLNavigationController *threeNav = [[MLNavigationController alloc] initWithRootViewController:threeVC]; [self.viewControllers addObject:oneNav]; [self.viewControllers addObject:twoNav]; [self.viewControllers addObject:threeNav];}

~8.4 增添子调控器,初叶化tabbarItem 方法

 - addController:(UIViewController *)viewController title:(NSString *)title normolImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName{ viewController.tabBarItem.title = title; UIImage *normalImage = [UIImage imageNamed:normalImageName]; normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; viewController.tabBarItem.image = normalImage; UIImage *selectedImage = [UIImage imageNamed:selectImageName]; selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; viewController.tabBarItem.selectedImage = selectedImage; [self addChildViewController:viewController];}

~8.5 设置文字差异情况下属性方法

 - setUpTabbarItemTextAttributes{ //普通状态下的文字属性 NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary]; normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.00f green:0.64f blue:0.51f alpha:1.00f]; //选中状态下的文字属性 NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor]; UITabBarItem *tabbarItem = [UITabBarItem appearance]; [tabbarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal]; [tabbarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];}

~8.6 UITabBarControllerDelegate代理方法

 - tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ if ([viewController isKindOfClass:[UINavigationController class]]) { UINavigationController *nav = (UINavigationController *)viewController; UIViewController *vc = [nav.viewControllers objectAtIndex:0]; NSLog(@"current viewcontroller is %@", vc); } return YES;}

~8.7 tabbar items 各属性赋值

NSArray *array = @[@{@"title":@"杀敌",@"normalImageName":@"battle_win_kill",@"selectImageName":@"battle_lose_kill"}, @{@"title":@"死亡",@"normalImageName":@"battle_win_death",@"selectImageName":@"battle_lose_death"}, @{@"title":@"助攻",@"normalImageName":@"battle_win_assist",@"selectImageName":@"battle_lose_assist"}]; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSDictionary *dic = (NSDictionary *)obj; UIViewController *vc = self.viewControllers[idx]; NSString *title = [dic objectForKey:@"title"]; NSString *normalImageName = [dic objectForKey:@"normalImageName"]; NSString *selectImageName = [dic objectForKey:@"selectImageName"]; [self addController:vc title:title normolImageName:normalImageName selectImageName:selectImageName]; }]; [self setUpTabbarItemTextAttributes]; self.delegate = self;
  • 9.至此基本截至,在目的调整器里面,能够接纳性的兑现基类的一对方法来自定义指标调节器,如:①目的调控器的navTitle<pre>[self
    setNavTitle:@”杀敌”];</pre>②是还是不是有重返开关,默许无<pre>[self
    setShowRightButton:YES];</pre>③设置navigationItem.rightBarButtonItem的图标<pre>[self.rightButton
    setImage:[UIImage imageNamed:@”battle_win_death”]
    forState:UIControlStateNormal];</pre>④rightBarButtonItem的点击响应放法<pre>-
    rightBarButtonItemTouchedUpInSide{//your code}</pre>

4、UITabBarController

  • tabbar调整器,同样是常用的分界面切换格局,一般作为app的根分界面包车型大巴视图调节器。其实与其说UITabBarController的分界面跳转,不及说是分界面切换,因为UITabBarController的分界面跳转其实正是UITabBarController的viewControllers数组中的多少个分界面切换。只要设置好了UITabBarController的viewControllers数组就足以了。也得以工厂自定义tabbar,通过selectedItem来调整。
  • 布局也近乎NavigationController

    图片 3TabBar的结构

网站地图xml地图