`
810364804
  • 浏览: 783839 次
文章分类
社区版块
存档分类
最新评论

用代码创建UITabBarController

 
阅读更多

文章目的

如何用纯代码的方式创建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<wbr>Finished<wbr>Selected<wbr>Image:(<code><a target="_blank" href="file:///Applications/Xcode.app/Contents/PlugIns/IDEQuickHelp.ideplugin/Contents/Resources/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIImage" title="" data-line="" data-column="" style="color: rgb(0, 51, 102); text-decoration: none; ">UI<wbr>Image</wbr></a> *</code>)<code>selected<wbr>Image</wbr></code> with<wbr>Finished<wbr>Unselected<wbr>Image:(<code><a target="_blank" href="file:///Applications/Xcode.app/Contents/PlugIns/IDEQuickHelp.ideplugin/Contents/Resources/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIImage" title="" data-line="" data-column="" style="color: rgb(0, 51, 102); text-decoration: none; ">UI<wbr>Image</wbr></a> *</code>)<code>unselected<wbr>Image</wbr></code></wbr></wbr></wbr></wbr></wbr></wbr>
这是UITabBarItem提供的方法,可以设定上面所显示的图片,selectedImage是只目前选择并显示的TabBatItem显示的图片

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<wbr>Title<wbr>Text<wbr>Attributes:(<code>NS<wbr>Dictionary *</wbr></code>)<code>attributes</code> for<wbr>State:(<code>UI<wbr>Control<wbr>State</wbr></wbr></code>)<code>state</code></wbr></wbr></wbr></wbr>
这个方法可以设定显示文字的属性,在这段代码中,是设定为显示文字的颜色





范例下载


其他参考

poolo的tabbar超过五个,如何修改more

分享到:
评论

相关推荐

    可自定义UITabBarController(iOS源代码)

    最少仅需两行代码(可使用Storyboard)创建自定义不仅可以通过代码创建UITabBarController, 还支持使用Storyboard创建UITabBarController, 及设置其tabBarItem的标题、图片(选中状态的图片) 以实现你自己的Style....

    UITabBarController范例代码

    如何用纯代码的方法创建并修改UITabBarController

    iOS应用开发中UITabBarController标签栏控制器使用进阶

    做了这么长时间的ios开发了,最基本的UITabBarController和UINavigationController都用了好长时间了,总是改...1. 在appDelegate里面创建UITabBarController; 准备好ViewControllerArray等其它数据变量; [/code] UITa

    UITabBarControllerDemo

    这是我在学习ios的过程中写的一个demo,使用代码的方式创建UITabBarController并添加子控制器。

    ios-最好的自定义UITabBarController之一.zip

    最少仅需两行代码(可使用Storyboard)创建自定义UITabBar样式 多种可DIY参数(UITabBar高度, Badge颜色大小位置, 小圆点, 滑动滑块)等等 有详细的Demo, Objective-C项目的两个, Swift项目的一个 最新版可前往...

    ios-swift3.0全面模仿系统tabBarController.zip

    githhub地址: https://github.com/GRSource/GRTabBarController 如果喜欢,就关注一下,谢谢,持续更新。最新代码上github下载。

    ios-两行代码快速创建一个iOS主流UI框架.zip

    本框架适用于 使用 NavigationController UITabBarController 的APP 框架QLSNavTab , GitHub地址:https://github.com/qianlishun/QLSNavTab 简介  -------准备工作------- 在AppDelegate中 设置一个...

    Swift5 iOS深浅模式适配

    swift5 Demo,Xcode11创建项目,支持iOS13深浅模式图片以及颜色动态变化。 项目为标准的UITabBarController布局,里面含有Alamofire5最新版本的网络请求,目前暂无官方文档。项目下载修改一下工程名就可以作为新项目...

    适用于iOS的自定义TabBar控制器-Swift开发

    目前仅与Storybo AZTabBarController一起使用AZTabBarController帮助扩展默认UITabBarController的功能,并创建具有自定义外观(带有动画,布局等)的自定义UITabBar项目。 现在仅适用于情节提要设置(因此您的...

    iOS Tabbar中间添加凸起可旋转按钮功能

    ![效果图.gif] ##需求分析 * tabbar有5个item,每个对应一个...我们一步步来解决这个问题,首先创建MCTabBarController继承UITabBarController,然后和常规一样创建5个item,中间的按钮不设置图片,代码如下 //MCT

Global site tag (gtag.js) - Google Analytics