UITabBarItem title 和self.title设置不同的标题

本文解决了一个在Swift开发中常见的问题,即如何分别设置UINavigationController的NavigationBar标题和UITabBarController的TabBar标题,而不让两者相互影响。通过使用self.navigationItem.title, self.tabBarItem.title和self.title属性,可以实现对导航栏和标签栏标题的独立控制。

转载自:  https://blog.youkuaiyun.com/chmod_R_755/article/details/50819634

问题解决了,首先吐槽下百度,翻了好几页都没找到,Google一下就搞定了,Google不在天朝发展对中国程序员来说损失不少时间。。。

原文

http://stackoverflow.com/questions/21615637/how-to-change-uiviewcontroller-title-independent-of-tabbar-item-title

经典部分:


self.navigationItem.title = @"my title"; sets navigation bar title.

self.tabBarItem.title = @"my title"; sets tab bar title.

self.title = @"my title"; sets both of these.
--------------------- 
作者:chmod_R_755 
来源:优快云 
原文:https://blog.youkuaiyun.com/chmod_R_755/article/details/50819634 
版权声明:本文为博主原创文章,转载请附上博文链接!

#import "TPNavigationController.h" #import "TPContainerViewController.h" #import "TPContainerNavigationController.h" #import "UIViewController+TPNavigationExtension.h" @interface TPContainerViewController () @property (nonatomic, strong) __kindof UIViewController *rootViewController; @property (nonatomic, strong) TPContainerNavigationController *containerNavigationController; @end @implementation TPContainerViewController // 包装过程 + (instancetype)wrapViewController:(UIViewController *)viewController { return [[self alloc] initWithViewController:viewController]; } // 包装过程 - (instancetype)initWithViewController:(UIViewController *)viewController { if (self = [super init]) { // 先将viewController包装在TPContainerNavigationController下面 self.containerNavigationController = [TPContainerNavigationController wrapNavigationControllerWithViewController:viewController]; // 再将TPContainerNavigationController包装在TPContainerViewController下面 [self addChildViewController:self.containerNavigationController]; [self.containerNavigationController didMoveToParentViewController:self]; // 记录控制器 self.rootViewController = viewController; self.containerNavigationController.containerViewContorller = self; } return self; } - (void)dealloc { [self.containerNavigationController removeFromParentViewController]; self.containerNavigationController = nil; } - (void)viewDidLoad { [super viewDidLoad]; // 添加视图 [self.view addSubview:self.containerNavigationController.view]; self.containerNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.containerNavigationController.view.frame = self.view.bounds; } - (void)didMoveToParentViewController:(UIViewController *)parent { if (parent == nil) { [self.containerNavigationController removeFromParentViewController]; self.containerNavigationController = nil; } } - (BOOL)shouldAutorotate { return self.rootViewController.shouldAutorotate; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.rootViewController.preferredInterfaceOrientationForPresentation; } - (BOOL)becomeFirstResponder { return [self.rootViewController becomeFirstResponder]; } - (BOOL)canBecomeFirstResponder { return [self.rootViewController canBecomeFirstResponder]; } - (UIStatusBarStyle)preferredStatusBarStyle { return self.rootViewController.preferredStatusBarStyle; } - (BOOL)prefersStatusBarHidden { return self.rootViewController.prefersStatusBarHidden; } - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { return self.rootViewController.preferredStatusBarUpdateAnimation; } - (BOOL)hidesBottomBarWhenPushed { return self.rootViewController.hidesBottomBarWhenPushed; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return self.rootViewController.supportedInterfaceOrientations; } - (UITabBarItem *)tabBarItem { return self.rootViewController.tabBarItem; } - (NSString *)title { return self.rootViewController.title; } - (UIViewController *)childViewControllerForStatusBarStyle { return self.rootViewController; } - (UIViewController *)childViewControllerForStatusBarHidden { return self.rootViewController; } - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures { return self.rootViewController.preferredScreenEdgesDeferringSystemGestures; } - (UIViewController *)rootViewController { TPContainerNavigationController *containerNavController = self.childViewControllers.firstObject; return containerNavController.viewControllers.firstObject; } @end
最新发布
09-27
/ // HBBaseTabbarViewController.m // wechat // // Created by josh on 2025/3/21. // #import "HBBaseTabbarViewController.h" #import "HBCustomTabBar.h" #import "HBFriendLineViewController.h" #import "HBProfileViewController.h" #import "HBHomeViewController.h" #import "HBContactViewController.h" @interface HBBaseTabbarViewController () @end @implementation HBBaseTabbarViewController - (void)viewDidLoad { [super viewDidLoad]; // 替换默认的 TabBar [self setValue:[[HBCustomTabbar alloc] init] forKey:@"tabBar"]; // 设置子视图控制器 [self setupViewControllers]; } - (void)setupViewControllers { // 创建 4 个子视图控制器 HBHomeViewController *homeVC = [[HBHomeViewController alloc] init]; // homeVC.view.backgroundColor = [UIColor whiteColor]; homeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home-sel"]]; HBContactViewController *contactVC = [[HBContactViewController alloc] init]; // contactVC.view.backgroundColor = [UIColor whiteColor]; contactVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:[UIImage imageNamed:@"contact"] selectedImage:[UIImage imageNamed:@"tongxunlu-sel"]]; HBFriendLineViewController *frienVC = [[HBFriendLineViewController alloc] init]; frienVC.view.backgroundColor = [UIColor whiteColor]; frienVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"朋友圈" image:[UIImage imageNamed:@"pengyouquan"] selectedImage:[UIImage imageNamed:@"pengyouquan-sel"]]; HBProfileViewController *profileVC = [[HBProfileViewController alloc] init]; profileVC.view.backgroundColor = [UIColor whiteColor]; profileVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"me"] selectedImage:[UIImage imageNamed:@"me-sel"]]; // 将子视图控制器添加到 TabBarController self.viewControllers = @[homeVC, contactVC, frienVC, profileVC]; } 这一串代码的作用
03-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值