#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%f %f %f %f",_btn.frame.origin.x,_btn.frame.origin.y,_btn.frame.size.height,_btn.frame.size.width);
_btn.frame = CGRectMake(20, 20, 280, 280);
NSLog(@"%f %f %f %f",_btn.frame.origin.x,_btn.frame.origin.y,_btn.frame.size.height,_btn.frame.size.width);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在运行后 输出结果为:
2014-09-21 15:25:34.779 test[2473:80334] 20.000000 20.000000 30.000000 30.000000
2014-09-21 15:25:34.784 test[2473:80334] 20.000000 20.000000 30.000000 30.000000
Xcode6默认是勾选这两项的,去掉才能有效.不过,以后屏幕多了,用自动布局来适配屏幕是大势所趋,再用代码来适配将会是场噩梦,建议还是习惯用autolayout+sizeclasses适配屏幕
在storyboard里的视图上加入一个按钮,然后在ViewController里创建IBOutlet,输入三行代码。除了这个你还做了别的吗?
我也遇到这个问题了,是设置滚动视图的时候,打印出frame的height已经改了,但显示的时候还是按照拖到storyboard上的高度来的。
试试这个
-(void)viewDidAppear:(BOOL)animated
{
_btn.frame = CGRectMake(20, 20, 280, 280);
NSLog(@"%f %f %f %f",_btn.frame.origin.x,_btn.frame.origin.y,_btn.frame.size.height,_btn.frame.size.width);
}