AJ學IOS 之ipad開發qq空間項目橫豎屏幕適配

AJ分享,必須精品markdown

 

一:效果圖

先看效果
這裏寫圖片描述app

二:結構圖

如圖所示:
這裏寫圖片描述框架

其中用到了UIView+extension分類
Masonry第三方框架作子控制器的適配
NYHomeViewController對應主頁也就是所有
NYDock是左邊的菜單欄 放置各個選項卡等按鈕功能區域
NYIconView頭像
NYTabBar選項卡
NYToolBar最下面的功能區
NYTabBarButton自定義的tabbar按鈕,爲了讓在橫豎屏下面分別顯示不一樣的樣式dom

三:源碼

NYHomeViewController

//
// NYHomeViewController.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/17.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYHomeViewController.h"
#import "NYDock.h"

#import "NYConst.h"

#import "Masonry.h"

@interface NYHomeViewController ()
@property (nonatomic, weak) NYDock *dock;

/** * 正在顯示的控制器 */
@property (nonatomic, weak) UIViewController *showingChildVc;
@end

@implementation NYHomeViewController

-(NYDock *)dock
{
    if (!_dock) {
        //建立dock
        NYDock *dock = [[NYDock alloc]init];
        [self.view addSubview:dock];
        self.dock = dock;

    }
    return _dock;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //設置背景色
    self.view.backgroundColor = NYColor(55, 55, 55);

    //初始化子控制器
    [self setupChildVcs];

    //接受通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarDidSelect:) name:NYTabBarDidSelectNotification object:nil];

    //根據屏幕方向控制dock屬性
    [self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0];
}



- (void)setupChildVcs
{
    for (int i = 0; i < 6; i++) {
        UIViewController *vc = [[UIViewController alloc]init];
        vc.view.backgroundColor = NYRandomColor;
        [self addChildViewController:vc];

    }
    //設置第一個爲默認選中
    [self switchChildVc:0];
}

/** * 切換子控制器 * * @param index 控制器索引 */
- (void)switchChildVc:(int)index
{
    //移除當前正在顯示的子控制器
    [self.showingChildVc.view removeFromSuperview];

    //顯示index對應的子控制器
    UIViewController *newChildVc = self.childViewControllers[index];
    [self.view addSubview:newChildVc.view];
    self.showingChildVc = newChildVc;

    // 添加約束
    [newChildVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        // make.width.mas_equalTo(600);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.dock.mas_right);
    }];

}
- (void)tabBarDidSelect:(NSNotification *)notification
{
    int index = [notification.userInfo[NYTabBarDidSelectIndex] intValue];
    [self switchChildVc:index];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/**當設備旋轉時候調用*/
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {//橫屏
        self.dock.width = NYDockLW;
        self.dock.height = NYScreenPW;

    }else{//豎屏
        self.dock.width = NYDockPW;
        self.dock.height = NYScreenLW;
    }
}

//-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
//{
// self.dock.width = size.width;
// self.dock.height = size.height;
//}


@end

Dock

//
// NYDock.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/17.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYDock.h"
#import "NYIconView.h"
#import "NYTabBar.h"
#import "NYToolBar.h"

#import "NYConst.h"
@interface NYDock()
@property (nonatomic, weak) NYIconView *iconView;
@property (nonatomic, weak) NYTabBar *tabBarView;
@property (nonatomic, weak) NYToolBar *toolBarView;
@end

@implementation NYDock

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        //1.頭像
        NYIconView *iconView = [[NYIconView alloc]init];
        [self addSubview:iconView];
        self.iconView = iconView;
// iconView.backgroundColor = [UIColor grayColor];
        //2.tobBar
        NYTabBar *tabBarView = [[NYTabBar alloc]init];
        [self addSubview:tabBarView];
        self.tabBarView = tabBarView;
// tabBarView.backgroundColor = [UIColor grayColor];

        //3.toolBar
        NYToolBar *toolBarView = [[NYToolBar alloc]init];
        [self addSubview:toolBarView];
// toolBarView.backgroundColor = [UIColor grayColor];
        self.toolBarView = toolBarView;

    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    self.toolBarView.width = self.width;

    self.tabBarView.width = self.width;
    self.tabBarView.height = NYDockPW * 6;


    if (Lanscape) {//橫
        self.toolBarView.height = self.width / 3;
        self.toolBarView.y = self.height - self.toolBarView.height;

        self.tabBarView.y = self.toolBarView.y - self.tabBarView.height;

        //頭像位置
        self.iconView.x = (self.width - self.iconView.width) * 0.5;
        self.iconView.y = self.iconView.x;
        self.iconView.width = self.width *0.4;
        self.iconView.height = self.iconView.width + 20;

    }else {//豎
        self.toolBarView.height = self.width * 3;
        self.toolBarView.y = self.height - self.toolBarView.height;

        self.tabBarView.y = self.toolBarView.y - self.tabBarView.height;

        //頭像位置
        self.iconView.x = 5;
        self.iconView.y = 50;
        self.iconView.width = self.width - 5;
        self.iconView.height = self.iconView.width;
    }

}

@end

IconView

//
// NYIconView.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/18.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYIconView.h"

#import "NYConst.h"
@implementation NYIconView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setImage:[UIImage imageNamed:@"cat"] forState:UIControlStateNormal    ];
        [self setTitle:@"znycat" forState:UIControlStateNormal];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.layer.cornerRadius = 10;
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if (Lanscape) {
        self.titleLabel.hidden = NO;
        self.imageView.width = self.width;
        self.imageView.height = self.width;
        self.imageView.y = 0;
        self.imageView.x = 0;

        self.titleLabel.width = self.width;
        self.titleLabel.height = self.height - self.width;
        self.titleLabel.y = self.width + 4;
        self.titleLabel.x = 0;

    }else{
        self.titleLabel.hidden = YES;
        self.imageView.bounds = self.bounds;
    }
}
@end

xtabBarView

//
// NYTabBar.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/18.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYTabBar.h"
#import "NYTabBarButton.h"

@interface NYTabBar()
@property (nonatomic, weak) NYTabBarButton *selectedBtn;
@end

@implementation NYTabBar

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //建立按鈕
        self.selectedBtn = [self setupButton:@"tabbar_photo"title:@"111"];
        self.selectedBtn.enabled = NO;
        [self setupButton:@"tabbar_mood"title:@"222"];
        [self setupButton:@"tabbar_blog"title:@"333"];
        [self setupButton:@"tabbar_photo"title:@"444"];
        [self setupButton:@"tabbar_mood"title:@"555"];
        [self setupButton:@"tabbar_blog"title:@"666"];
    }
    return self;
}

/** * 建立按鈕 */
- (NYTabBarButton *) setupButton:(NSString *)icon title:(NSString *)title
{
    NYTabBarButton *button = [[NYTabBarButton alloc]init];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
// button.contentMode(image的是contentModel)對內容無用

    [self addSubview:button];
    return button;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    int count = self.subviews.count;

    for (int i = 0 ; i<count; i++) {
        NYTabBarButton *btn = self.subviews[i];
        btn.height = self.height / count;
        btn.width = self.width ;
        btn.x = 0;
        btn.y = btn.height * i;
        btn.tag = i;

        if (Lanscape) {
            btn.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        }else{
            btn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        }

    }


}

-(void)btnClick:(NYTabBarButton *)btn
{
    self.selectedBtn.enabled = YES;
    btn.enabled = NO;
    self.selectedBtn = btn;

    //發送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:NYTabBarDidSelectNotification object:nil userInfo:@{NYTabBarDidSelectIndex:@(btn.tag)}];
}

@end

TabBarButton

//
// NYTabBarButton.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/18.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYTabBarButton.h"

@implementation NYTabBarButton

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateDisabled];
// 讓圖片不要拉伸
        self.imageView.contentMode = UIViewContentModeCenter;

        //不能選中和高亮時候的灰色去掉
        self.adjustsImageWhenDisabled = NO;
        self.adjustsImageWhenHighlighted = NO;
// //設置按鈕的內容樣式向左邊對其
// self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 
// // 切割間距內邊距
// self.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
// self.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    }
    return self;
}

-(void)setHighlighted:(BOOL)highlighted{
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (Lanscape) {
        self.imageView.height = self.height;
        self.imageView.width = self.width * 0.4;
        self.imageView.x = 0;
        self.imageView.y = 0;

        self.titleLabel.hidden = NO;
        self.titleLabel.y = 0;
        self.titleLabel.x = self.imageView.width;
        self.titleLabel.width = self.width - self.imageView.width;
        self.titleLabel.height = self.height;
    } else {
        self.titleLabel.hidden = YES;
        // 若是設置寬度或者高度爲0,某個控件仍是會顯示一部分, 能夠嘗試設置控件的width或者height爲負數
        // self.titleLabel.frame = CGRectMake(0, 0, -1, 0);
        self.imageView.frame = self.bounds;
    }
}

@end

ToolBarView

//
// NYToolBar.m
// QQ空間 橫豎屏適配
//
// Created by apple on 15/10/18.
// Copyright (c) 2015年 apple. All rights reserved.
//

#import "NYToolBar.h"


@implementation NYToolBar

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //建立按鈕
        [self setupButton:@"tabbar_photo"];
        [self setupButton:@"tabbar_mood"];
        [self setupButton:@"tabbar_blog"];
    }
    return self;
}

/** * 建立按鈕 */
- (UIButton *) setupButton:(NSString *)icon
{
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateSelected];
    [self addSubview:button];
    return button;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    int count = self.subviews.count;
    if (Lanscape) {
        for (int i = 0 ; i<count; i++) {
            UIButton *btn = self.subviews[i];
            btn.height = self.height;
            btn.width = self.width / count;
            btn.x = btn.width * i;
            btn.y = 0;
        }

    } else{
        for (int i = 0 ; i<count; i++) {
            UIButton *btn = self.subviews[i];
            btn.height = self.height / count;
            btn.width = self.width ;
            btn.x = 0;
            btn.y = btn.height * i;
        }
    }
}

@end
相關文章
相關標籤/搜索