美文网首页
4.8 微信界面搭建(UITabBarController)

4.8 微信界面搭建(UITabBarController)

作者: 草根小强 | 来源:发表于2019-04-12 17:35 被阅读0次

    微信界面搭建(UITabBarController)

    #import "AppDelegate.h"
    #import "ADTabBarController.h"
    #import "ViewController1.h"
    #import "ViewController2.h"
    #import "ViewController3.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        ViewController1 * vc1 = [[ViewController1 alloc] init];
        vc1.view.backgroundColor = [UIColor redColor];
        
        ViewController2 * vc2 = [[ViewController2 alloc] init];
        vc2.view.backgroundColor = [UIColor yellowColor];
        
        ViewController3 * vc3 = [[ViewController3 alloc] init];
        vc3.view.backgroundColor = [UIColor blueColor];
        
        //分栏控制器
        ADTabBarController * tab = [[ADTabBarController alloc] init];
        tab.viewControllers = @[vc1, vc2, vc3];
        
        _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _window.backgroundColor = [UIColor whiteColor];
        _window.rootViewController = tab;
        [_window makeKeyAndVisible];
        
        return YES;
    }
    
    

    创建3个视图控制器

    创建UITabBarController

    #import <UIKit/UIKit.h>
    
    @interface ADTabBarController : UITabBarController
    
    @end
    
    
    #import "ADTabBarController.h"
    
    @implementation ADTabBarController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //隐藏系统tabbar
        self.tabBar.hidden = YES;
        //创建自己的tabbar
        [self buildTabbar];
    }
    
    - (void)buildTabbar {
        UIImageView * tabbarBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 49, [[UIScreen mainScreen] bounds].size.width, 49)];
        //打开用户交互
        tabbarBottom.userInteractionEnabled = YES;
        tabbarBottom.image = [UIImage imageNamed:@"header_bg"];
        tabbarBottom.tag = 111;
        
        //添加按钮元素
        NSArray * titles = @[@"消息", @"朋友", @"动态"];
        NSArray * images = @[@"tab_faxian_normal", @"tab_friend_normal", @"tab_set_normal"];
        NSArray * selectedImages = @[@"tab_faxian_select", @"tab_friend_select", @"tab_set_select"];
        
        for (int i = 0; i < titles.count; i++) {
            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width / titles.count * i, 0, [[UIScreen mainScreen] bounds].size.width / titles.count, 49)];
            //设置标题
            [button setTitle:titles[i] forState:UIControlStateNormal];
            //设置标题颜色
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
            //更改字体大小
            button.titleLabel.font = [UIFont systemFontOfSize:12];
            //设置图片
            [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:selectedImages[i]] forState:UIControlStateSelected];
            //绑定事件
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            //容器视图控制器和其子视图控制器中的任意视图tag值不能重复
            button.tag = 100 + i;
            
            
            //调整标题与图标的位置
            /*
             UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
             UIEdgeInsets insets = {top, left, bottom, right};
             return insets;
             }
             */
            
            //UIEdgeInsets top:向上为负 向下为正 left:向左为负,向右为正  bottom:向下为负,向上为正 right:向右为负,向左为正   总结: 上左下右: 是哪个方向就向哪个方向为负,即:top:向上为负,向下为正
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.bounds.size.width, -20, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, -button.titleLabel.bounds.size.width);
    
            
            [tabbarBottom addSubview:button];
            
            //设置默认选中项,为0
            if (i == 0) {
                [self buttonClicked:button];
            }
        }
        
        
        [self.view addSubview:tabbarBottom];
    }
    
    - (void)buttonClicked:(UIButton *)button {
       
       
        NSInteger index = button.tag - 100;
        
        self.selectedIndex = index;
        
        //让选中的按钮处于选中状态
        UIImageView * imageView = (id)[self.view viewWithTag:111];
        NSArray * subViews = imageView.subviews;
        for (UIButton * button in subViews) {//这里循环的目的就是让之前按钮状态变为非选中状态,然后只选中一个之后,就是选中状态了。
            [button setSelected:NO];
        }
        //被点击的按钮设置为选中状态
        [button setSelected:YES];
    }
    
    @end
    
    
    微信界面搭建(UITabBarController).png

    相关文章

      网友评论

          本文标题:4.8 微信界面搭建(UITabBarController)

          本文链接:https://www.haomeiwen.com/subject/nhxhwqtx.html