ios - view 里面的 view 怎么设置frame
巴扎黑
巴扎黑 2017-04-17 13:39:57
[iOS讨论组]

在storyboard中我添加了三个view

当旋转的时候,我希望他变成这样

但实际上变成了这样

试了多次,结论是不知道view里面的view怎么用代码设置frame
层级关系是这样的:

代码如下:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *b1;
@property (weak, nonatomic) IBOutlet UIView *b2;
@property (weak, nonatomic) IBOutlet UIView *b3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if ([UIScreen mainScreen].bounds.size.width > [UIScreen mainScreen].bounds.size.height) {
        _b1.frame = CGRectMake(16, 220, 120, 50);
        _b2.frame = CGRectMake(16, 278, 160, 70);
        _b3.frame  = CGRectMake(16, 336, 200, 90);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    //if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    if(interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        _b1.frame = CGRectMake(10, 10, 50, 50);
        _b2.frame = CGRectMake(10, 10, 70, 70);
        _b3.frame  = CGRectMake(16, 20, 90, 90);
    }
    else
    {
        _b1.frame = CGRectMake(16, 80, 120, 50);
        _b2.frame = CGRectMake(16, 138, 160, 70);
        _b3.frame  = CGRectMake(16, 196, 200, 90);
    }

}

@end

赐教!

更新:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *b1;
@property (weak, nonatomic) IBOutlet UIView *b2;
@property (weak, nonatomic) IBOutlet UIView *b3;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
if ([UIScreen mainScreen].bounds.size.width > [UIScreen mainScreen].bounds.size.height) {
_b3.frame = CGRectMake(16, 200, 200, 90);
_b2.frame = CGRectMake(10, 10, 180, 70);
_b1.frame = CGRectMake(10, 10, 160, 50);
}
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
    {
    //if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    if(interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
    _b3.frame = CGRectMake(16, 20, 90, 90);
    _b2.frame = CGRectMake(10, 10, 70, 70);
    _b1.frame = CGRectMake(10, 10, 50, 50);
    }
    else
    {
    _b3.frame = CGRectMake(16, 200, 200, 90);
    _b2.frame = CGRectMake(10, 10, 180, 70);
    _b1.frame = CGRectMake(10, 10, 160, 50);
    }

}

@end


巴扎黑
巴扎黑

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号