猫猫学iOS之ipad开发qq空间项目横竖屏幕适配

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客

地址:http://blog.csdn.net/u013357243

一:效果图

先看效果

二:结构图

如图所示:

其中用到了UIView+extension分类

Masonry第三方框架做子控制器的适配

NYHomeViewController对应主页也就是全部

NYDock是左边的菜单栏 放置各个选项卡等按钮功能区域

NYIconView头像

NYTabBar选项卡

NYToolBar最下面的功能区

NYTabBarButton自定义的tabbar按钮,为了让在横竖屏下面分别显示不同的样式

三:源码

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 01:30:11

猫猫学iOS之ipad开发qq空间项目横竖屏幕适配的相关文章

猫猫学iOS之ipad开发Popover的调色板应用_popover显示后其他控件仍然能进行交互

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 后面的是xcode的控制台 二:代码 ViewController #import "ViewController.h" #import "ColorsViewController.h" @interface ViewController () <ColorsViewControllerDelegate, UIPop

猫猫学iOS之ipad开发Popover的基本使用

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果图 二:注意 对于方法[UIPopoverController dealloc] reached while popover is still visible. 当popover还在显示的时候,它就挂了 不允许popover还在显示的时候挂掉 popover必须在消失的时候挂掉 三:核心代码 - (IBAction)popMenu:(id)item {

(素材_源码) 猫猫学IOS(五)UI之360等下载管理器九宫格UI

猫猫分享,必须精品 先看效果 代码学习地址: 猫猫学IOS(五)UI之360等下载管理器九宫格UI 猫猫学IOS(五)UI之360等下载管理器九宫格UI http://blog.csdn.net/u013357243/article/details/44486609 下载地址:http://download.csdn.net/detail/u013357243/8516817 ps1:有想要源码的可以加猫猫微信znycat QQ也可以:1764541256 --视频学习资料素材免费分析,哎自己一

猫猫学IOS(四)UI之半小时搞定Tom猫

话不多说 先上效果 项目源码素材下载地址: Tom猫游戏代码iOS 素材http://blog.csdn.net/u013357243/article/details/44457357 效果图 曾经风靡一时的tom猫其实制作起来那是叫一个相当的easy啊 功能全部实现,(关键是素材,没有素材的可以加我微信) 新手也可以很快的完成tom这个很拉轰的ios应用哦 做过android的我表示,android党默哀下把,那个做起来真心痛苦.... 然后呢你需要准备这些素材... 拖拽控件吧,因为这一个项

IOS开发--iPad之QQ空间 :(一)搭建登录界面

登录界面效果图: 相关的图片资源下载百度云备份链接: http://pan.baidu.com/s/1o71cvMU 密码: 2h7e 步骤开始: 设置辅助窗口的位置在下方 快捷键option,然后拖拽复制之后: 这里就直接省去了将背景颜色改为经典黑了. 到这里QQ空间的登录界面搭建完毕.

猫猫学iOS之最近的反思

其实很早以前就想写点了,虽然猫猫现在还是学生,但是就自学方面,猫猫觉得自己水平还可以--注意,我不是大神,我只是一名小小的菜猫... 首先简单说一下自己吧,本人猫猫,真名看博客名字,目前大四,自学编程两年半多了,学习路线: 易语言 –> 啊哈 –> java –> javaWeb(jstl,html,js,css-..) –> android –> c –> objective-c –> iOS.作为一名体育生表示不务正业的学了这么多程序猿的东东...不多说了 言

猫猫学IOS(十三)UI之UITableView学习(下)汽车名牌带右侧索引

猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/44727225 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 代码 ViewController //ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧. 原文:http://blog.csdn.net/u0133

猫猫学iOS 之微博项目实战(2)微博主框架-自己定义导航控制器NavigationController

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 一:加入导航控制器 上一篇博客完毕了对底部的TabBar的设置,这一章我们完毕自己定义导航控制器(NYNavigationController). 为啥要做自己定义呢.由于为了更好地封装代码,而且系统的UINavigationController不能满足我们的需求了,所以得自己定义. 首先,我们在NYTabBarViewCon

iOS(iPhone,iPad))开发(Objective-C)开发库常用库索引

http://www.code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 http://www.cocoacontrols.com/ 英文版本的lib收集 http://www.objclibs.com/ 精品lib的收集网站 http://www.ityran.com/forum-61-1.html 泰然代码仓库 ---------------------- emoji ---------------------- http://www.easyapns.c