在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)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


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