ios横竖屏配置subViews
2015-09-02 15:32
351 查看
ayoutSubviews的调用时机:
1.init时不会被调用、
2.将要真正显示的会调用
3.frame发现改变时智能调用
4.滚动、旋转、remove等等时
这些时机都是和frame相关的,也是唯一能更新子视图的最好时机
layoutSubviews的调用时机:http://blog.csdn.net/zhangao0086/article/details/6904854
drawRect是在视图被真正显示的时候,如果layer的contents没有内容,就会被调用
横竖屏demo:
A:----views
@interface DetialViews ()<UIWebViewDelegate>{
UIView *bgView;
UIWebView *webView;
}
@end
@implementation DetialViews
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview{
if (newSuperview !=nil) {
if ([self respondsToSelector:@selector(setupViews)]) {
[self setupViews];
}
if ([self respondsToSelector:@selector(addSubviews)]) {
[self addSubviews];
}
}
}
- (void)layoutSubviews{
[super layoutSubviews];
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationPortrait:{
if ( [self respondsToSelector:@selector(layoutPortrait)]) {
[self layoutPortrait];
}
}
break;
case UIInterfaceOrientationLandscapeRight:
{
if ([self respondsToSelector:@selector(layoutLandscape)]) {
[self layoutLandscape];
}
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
default:
break;
}
}
- (void)setupViews{
bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor blueColor];
webView = [[UIWebView alloc] init];
webView.backgroundColor = [UIColor whiteColor];
NSURL *url =[NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
- (void)addSubviews{
[self addSubview:bgView];
[self addSubview:webView];
}
- (void)layoutLandscape{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home右边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
- (void)layoutPortrait{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home下边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
B: viewController
#import "DetialVViewController.h"
#import "DetialViews.h"
@interface DetialVViewController (){
DetialViews * _detialView;
}
@end
@implementation DetialVViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
- (void)addSubViews{
_detialView = [[DetialViews alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_detialView];
}
1.init时不会被调用、
2.将要真正显示的会调用
3.frame发现改变时智能调用
4.滚动、旋转、remove等等时
这些时机都是和frame相关的,也是唯一能更新子视图的最好时机
layoutSubviews的调用时机:http://blog.csdn.net/zhangao0086/article/details/6904854
drawRect是在视图被真正显示的时候,如果layer的contents没有内容,就会被调用
横竖屏demo:
A:----views
@interface DetialViews ()<UIWebViewDelegate>{
UIView *bgView;
UIWebView *webView;
}
@end
@implementation DetialViews
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview{
if (newSuperview !=nil) {
if ([self respondsToSelector:@selector(setupViews)]) {
[self setupViews];
}
if ([self respondsToSelector:@selector(addSubviews)]) {
[self addSubviews];
}
}
}
- (void)layoutSubviews{
[super layoutSubviews];
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationPortrait:{
if ( [self respondsToSelector:@selector(layoutPortrait)]) {
[self layoutPortrait];
}
}
break;
case UIInterfaceOrientationLandscapeRight:
{
if ([self respondsToSelector:@selector(layoutLandscape)]) {
[self layoutLandscape];
}
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
default:
break;
}
}
- (void)setupViews{
bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor blueColor];
webView = [[UIWebView alloc] init];
webView.backgroundColor = [UIColor whiteColor];
NSURL *url =[NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
- (void)addSubviews{
[self addSubview:bgView];
[self addSubview:webView];
}
- (void)layoutLandscape{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home右边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
- (void)layoutPortrait{
[bgView setFrame:CGRectMake(0, 0, TjScreeenWeight, 60)];
[webView setFrame:CGRectMake(0, bgView.frame.size.height+bgView.frame.origin.y, self.frame.size.width, TjScreenHeight - bgView.frame.size.height )];
NSLog(@"home下边----->%f---%f",bgView.bounds.size.width,bgView.bounds.size.height);
}
B: viewController
#import "DetialVViewController.h"
#import "DetialViews.h"
@interface DetialVViewController (){
DetialViews * _detialView;
}
@end
@implementation DetialVViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
- (void)addSubViews{
_detialView = [[DetialViews alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_detialView];
}
相关文章推荐
- 【iOS开发系列】NSClassFromString、NSSelectorFromString、isKindOfClass、isMemberOfClass
- ios集成支付宝
- IOS的文件I/O
- iOS应用间共享数据
- 国内iOS界最大的行动已经开始了。全面开始翻译iOS开发者文档。
- IOS开发中NSRunloop跟NSTimer的问题
- IOS (null )和<null>的处理
- IOS 页面跳转+Delegate传值
- iOS开发有关头像上传的问题
- 初探 iOS8 中的 Size Class
- iOS计算缓存文件的大小
- iOS时间格式转换总结
- iOS TableView实现上拉加载更多数据
- iOS scrollview实现图片放大和缩小的功能
- iOS---开发实用传感器
- iOS开发路线简述
- iOS开发之检测设备锁屏
- 苹果开发 笔记(70)常用的字符串操作
- iOS进阶路线以及进阶书籍
- iOS 多线程与GCD