AJ分享,必须精品

一:效果图

先看效果

二:结构图

如图所示:

其中用到了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

AJ学IOS 之ipad开发qq空间项目横竖屏幕适配的更多相关文章

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

    AJ分享,必须精品 一:效果 后面的是xcode的控制台 二:代码 ViewController #import "ViewController.h" #import " ...

  2. AJ学IOS 之ipad开发Popover的基本使用

    AJ分享,必须精品 一:效果图 二:注意 对于方法[UIPopoverController dealloc] reached while popover is still visible. 当popo ...

  3. iPad开发--QQ空间,处理横竖屏布局,实现子控件中的代理

    一.主界面横竖屏效果图 二.主界面加载, 初始化Dock(红色框的控件),判断程序启动时的屏幕方向.调用自己- (void)transitionToLandScape:(BOOL)isLandScap ...

  4. iOS开发UI篇—模仿ipad版QQ空间登录界面

    iOS开发UI篇—模仿ipad版QQ空间登录界面 一.实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分 3.登陆界面的 ...

  5. iOS - 初学iPad开发入门

    iPad是一款苹果公司于2010年发布的平板电脑定位介于苹果的智能手机iPhone和笔记本电脑MacBook产品之间跟iPhone一样,搭载的是iOS操作系统 iPhone和iPad开发的区别 屏幕的 ...

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

    http://www.code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例  http://www.cocoacontrols.com/ 英文版本的lib收集  ht ...

  7. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  8. AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引

    AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...

  9. 48 (OC)* 适配iPad和iPhone、以及横竖屏适配。

    一:核心方法 1.三个方法 1.1:开始就会触发 - (void)viewWillLayoutSubviews; 1.2:开始就会触发 - (void)viewDidLayoutSubviews; 1 ...

随机推荐

  1. Journal of Proteome Research | iHPDM: In Silico Human Proteome Digestion Map with Proteolytic Peptide Analysis and Graphical Visualizations(iHPDM: 人类蛋白质组理论酶解图谱的水解肽段分析和可视化展示)| (解读人:邓亚美)

    文献名:iHPDM: In Silico Human Proteome Digestion Map with Proteolytic Peptide Analysis and Graphical Vi ...

  2. scapy学习笔记

    1.ACK Scan >>>ans,unans=sr(IP(dst="www.baidu.com")/TCP(dport=[80,666],flags=" ...

  3. matplotlib.pyplot.text

    matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=<deprecated parameter>, **kwargs)[sour ...

  4. 粒子群优化算法(PSO)之基于离散化的特征选择(FS)(二)

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 作者:Geppetto 前面我们介绍了特征选择(Feature S ...

  5. Vue路由配置history模式

    我的博客: https://github.com/Daotin/fe-notes/issues vue需要node.js吗? 你可以用 script 标签的形式引入vue.min.js 这样的,不需要 ...

  6. PHP7内核(六):变量之zval

    记得网上流传甚广的段子"PHP是世界上最好的语言",暂且不去讨论是否言过其实,但至少PHP确实有独特优势的,比如它的弱类型,即只需要$符号即可声明变量,使得PHP入手门槛极低,成为 ...

  7. Jmeter接口测试之用户自定义变量(九)

    在使用Jmeter做接口自动化测试中,经常会使用到公共的数据,那么就需要对这些公共的数据分离出来,不管是基于测试框架的思想,还是使用工具来进行做自动化测试,公共数据的分离首先是需要思考的.这里就以获取 ...

  8. iOS sign in with Apple 苹果ID登录

    http://www.cocoachina.com/articles/109104?filter=ios https://juejin.im/post/5deefc5e518825126416611d ...

  9. Java程序员必读的9本书

    本文列出的9本书在Java程序员界都是被认为很棒的书.当一个程序员开始初学Java时,他的第一个问题应该是如何选择一本书来作为指导学习Java.这个问题也就表明,相对于其他的教程和博客,Java书籍还 ...

  10. Python学习笔记:String类型所有方法汇总

    # 按字母表熟悉下string中的方法# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z# 标红的为常用重点的方法!! str = " ...