用代码创建UITabBarController
来源:互联网 发布:手机淘宝旺旺名在哪里 编辑:程序博客网 时间:2024/06/05 11:49
文章目的
如何用纯代码的方式创建UITabBarController
方法
1. 首先开启XCode并建立一个Empty Application
2. 加入一个Objective-C Class并继承自UIViewController,取名为FirstViewController
3. 重复一次上面动作,并取名为SecondViewController
4. 之后可在工程中看到此图
5. 开启AppDelegate.h,并加入如下代码
#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>{ UITabBarController* tabBarViewController;}@property (strong, nonatomic) UIWindow *window;@end
6. 开启AppDelegate.m,并加入如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; tabBarViewController = [[UITabBarController alloc]init]; [self.window setRootViewController:tabBarViewController]; FirstViewController* first = [[FirstViewController alloc]init]; SecondViewController* second = [[SecondViewController alloc]init]; tabBarViewController.viewControllers = [NSArray arrayWithObjects:first, second, nil]; [first release]; [second release];}
tabBarViewController = [[UITabBarController alloc]init]; [self.window setRootViewController:tabBarViewController];第一行代码为初始一个UITabBarController
第二行为将tabBarViewController设定为window的root view controller(根视图控制器)
tabBarViewController.viewControllers = [NSArray arrayWithObjects:first, second, nil];接下来利用UITabBarController的viewControllers属性,设定First及Second两个控制器。
7.用代码修改TabBar与TabBarItem
UITabBar *tabBar = tabBarViewController.tabBar;UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];UITabBarController提供一个tabBar属性,我们可以透过这个属性取得UITabBar
并在UITabBar的items属性取得所有的UITabBarItem
tabBarItem1.title = @"Home";tabBarItem2.title = @"Maps";透过UITabBarItem的title属性,可以设定tab bar item上显示的文字
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];
- (void)set这是UITabBarItem提供的方法,可以设定上面所显示的图片,selectedImage是只目前选择并显示的TabBatItem显示的图片Finished Selected Image:( UI
)Image *selected
withImage Finished Unselected Image:( UI
)Image *unselected
Image
unselectedImage则是平常未选中时显示的图片
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];[[UITabBar appearance] setBackgroundImage:tabBarBackground];这段代码可以修改UITabBar显示的背景图
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];这可以设定选中的UITabBarItem后面的图
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: titleHighlightedColor, UITextAttributeTextColor, nil] forState:UIControlStateHighlighted];
- (void)set这个方法可以设定显示文字的属性,在这段代码中,是设定为显示文字的颜色Title Text Attributes:( NS
)Dictionary * attributes
forState:( UI
)Control State state
范例下载
其他参考
poolo的tabbar超过五个,如何修改more
- 用代码创建UITabBarController
- 用代码创建UITabBarController
- 代码创建 UITabBarController
- [IOS]代码创建UINavigationController+UITabBarController
- iOS开发 代码创建UITabBarController
- 用 storyboard 创建 UITabBarController
- 使用代码和storyboard分别创建UITabBarController
- iOS 使用代码和storyboard分别创建UITabBarController
- iOS Swift创建UITabBarController
- UITabBarController UINavigationController 相结合代码
- UITabBarController UINavigationController 相结合代码
- UITabBarController UINavigationController 相结合代码
- 【iOS开发-78】用代码实现UITabBarController+UINavigationController
- ios-day13-02(UITabBarController使用代码的方式创建并添加子控制器)
- 代码实现UITabBarController加载UINavigationController
- UITabbarController
- UITabBarController
- UITabBarController
- Android相机开发(一)之预览拍照查看图片基本功能的实现
- Matlab 安装libsvm-3.1工具箱介绍
- 【Deep Learning学习笔记】Modeling Documents with a Deep Boltzmann Machine_Hinton_uai2013
- css优化技巧
- MatPlotLib(Basemap)与Grads、NCL和GMT的对比
- 用代码创建UITabBarController
- Thinking in Java(第四版)习题--第三章
- hdu 2896 病毒侵袭 AC自动机
- glBlendFunc();--混合
- learning puppet 4--template and function
- Cgroup文件系统
- ubuntu下安装zend-framework
- sleep()和 wait()有什么区别
- 服务管理——telnet