b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

IOS开发入门之iOS 自定义UITabBarController

电脑杂谈  发布时间:2019-09-27 22:05:14  来源:网络整理

ios 自定义tabbar_ios 自定义tabbar_ios tabbar尺寸

本文将带你知道IOS开发入门iOS 自定义UITabBarController,希望本文对你们学IOS有所帮助。

UITabBarController的搭建

AppDelegate.m#application:didFinishLaunchingWithOptions主要分为三步:

1.创建窗体

2.设置根视图控制器

3.显示窗体

问题:所有代码提到AppDelegate中不够简介,方法太臃肿,而且①:UITabBarController默认会对tabBarItem选中状态的照片进行渲染成红色,②:标题的图标的色调也是白色,③:发布照片显示还有问题

抽取代码:<喎?"/kf/ware/vc/"target="_blank"class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">#import"BWTabBarController。h"#import"BWEssenceViewController。h"#import"BWFriendTrendViewController。h"#import"BWMeViewController。h"#import"BWNewViewController。h"#import"BWPublishViewController。h"@interfaceBWTabBarController()@end@implementationBWTabBarController-(void)viewDidLoad{[superviewDidLoad];[selfaddAllChildViewController];[selfstupAllTabBarItemTitleAndImage];}//添加子视图控制器-(void)addAllChildViewController{//2。

1精华BWEssenceViewController*essenceViewController=[[BWEssenceViewControlleralloc]init];UINavigationController*essenceNavigationController=[[UINavigationControlleralloc]initWithRootViewController:essenceViewController];[selfaddChildViewController:essenceNavigationController];//2。2新帖BWNewViewController*newViewController=[[BWNewViewControlleralloc]init];UINavigationController*newNavigationController=[[UINavigationControlleralloc]initWithRootViewController:newViewController];[selfaddChildViewController:newNavigationController];//2。

3发布BWPublishViewController*publishViewController=[[BWPublishViewControlleralloc]init];[selfaddChildViewController:publishViewController];//2。4关注BWFriendTrendViewController*friendTrendViewController=[[BWFriendTrendViewControlleralloc]init];UINavigationController*friendTrendNavigationController=[[UINavigationControlleralloc]initWithRootViewController:friendTrendViewController];[selfaddChildViewController:friendTrendNavigationController];//2。5我BWMeViewController*meViewController=[[BWMeViewControlleralloc]init];UINavigationController*meNavigationController=[[UINavigationControlleralloc]initWithRootViewController:meViewController];[selfaddChildViewController:meNavigationController];}-(void)stupAllTabBarItemTitleAndImage{//1。

精华UINavigationController*essenceNavigationController=self。childViewControllers[0];essenceNavigationController。tabBarItem。title=@"精华";essenceNavigationController。tabBarItem。image=[UIImageimageNamed:@"tabBar_essence_icon"];essenceNavigationController。tabBarItem。selectedImage=[UIImageimageNamed:@"tabBar_essence_click_icon"];//2。新帖UINavigationController*newNavigationController=self。childViewControllers[1];newNavigationController。tabBarItem。title=@"新帖";newNavigationController。

tabBarItem。image=[UIImageimageNamed:@"tabBar_new_icon"];newNavigationController。tabBarItem。selectedImage=[UIImageimageNamed:@"tabBar_new_click_icon"];//3。发布BWPublishViewController*publishViewController=self。childViewControllers[2];publishViewController。tabBarItem。image=[UIImageimageNamed:@"tabBar_publish_icon"];publishViewController。tabBarItem。selectedImage=[UIImageimageNamed:@"tabBar_publish_click_icon"];//关注UINavigationController*friendTrendNavigationController=self。

childViewControllers[3];friendTrendNavigationController。tabBarItem。title=@"关注";friendTrendNavigationController。tabBarItem。image=[UIImageimageNamed:@"tabBar_friendTrends_icon"];friendTrendNavigationController。tabBarItem。selectedImage=[UIImageimageNamed:@"tabBar_friendTrends_click_icon"];//我UINavigationController*meNavigationController=self。childViewControllers[4];meNavigationController。tabBarItem。title=@"我";meNavigationController。tabBarItem。image=[UIImageimageNamed:@"tabBar_me_icon"];meNavigationController。tabBarItem。selectedImage=[UIImageimageNamed:@"tabBar_me_click_icon"];}@end

AppDelegate

-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{

//1.创建窗口

self.window=[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

//2.设置窗口的根进而控制器

UITabBarController*tabBarController=[[UITabBarControlleralloc]init];

self.window.rootViewController=tabBarController;

//3.显示窗口

[self.windowmakeKeyAndVisible];

returnYES;

}

ios 自定义tabbar_ios tabbar尺寸_ios 自定义tabbar

以上代码更加简单了,但是上述三个问题还没有解决:

UITabBarItem选中的截图被渲染的解决方法

方式一:设置截图的渲染模式

原因:UITabBarItem中Image的截图的默认的渲染模式是:Default方式,这样通过[UIImageimageNamed:]方式是获得渲染后的照片,将渲染后的照片赋值给UITabBarItem.selectedImage,所以能够出现此类问题;修改截图的渲染模式为原始图片即可:RenderAs:OriginalImage

具体操作:全选tabBar所有的截图并修改RenderAs:OriginalImage

方式二:使用代码模式

方式一预测了难题出现的根本因素是将渲染后的照片赋值给了tabBarItem.selectedImage,所以解决方法就是从UIImage对象中获得原始图片,然后直接赋值即可

UIImage*image=[UIImageimageNamed:@"tabBar_essence_click_icon"];

image=[imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//获取截图的原始图片

essenceNavigationController.tabBarItem.selectedImage=image;

可以提供一个类别

@implementationUIImage(Image)

+(UIImage*)imageOriginalWithName:(NSString*)name{

UIImage*image=[UIImageimageNamed:name];

return[imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}

@end

essenceNavigationController.tabBarItem.selectedImage=[UIImageimageOriginalWithName:@"tabBar_essence_click_icon"];

两种方法比较:

方式一简单,方式二稍微麻烦,但是方法一有也许美工换图后,忘记更改渲染模式而使用代码形式就没有这种想法

设置每个UITabBarItem选中状态的标题颜色

设置UITabBarItem选中状态下的标题颜色就能

NSMutableDictionary*attrs=[NSMutableDictionarydictionary];


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-124387-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...