CGRectMake不适用于viewDidLayoutSubviews中的自定义框架

时间:2013-04-08 18:36:24

标签: objective-c cocoa-touch ios6 interface-builder autolayout

这是我正在使用的代码:

-(void)viewDidLayoutSubviews{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;

    if (screenSize.height <= 480.0f) {
        self.img.frame = CGRectMake(21, 20, 280, 275);
        self.text.frame = CGRectMake(21, 20, 280, 275);
        self.decrease.frame= CGRectMake(55, 308, 39, 39);
        self.add.frame = CGRectMake(55, 308, 39, 39);
        self.increase.frame =CGRectMake(55, 308, 39, 39);
    }
    else{
        self.img.frame = CGRectMake(21, 20, 280, 400);
        self.text.frame = CGRectMake(21, 20, 280, 400);
        self.decrease.frame= CGRectMake(55, 308, 39, 39);
        self.add.frame = CGRectMake(55, 308, 39, 39);
    }    
}

我使用相同的代码(在viewDidLayoutSubviews之外)来改变帧并且它完美地工作。 我的问题是viewDidLayoutSubviews中的代码不起作用。加载视图时,它不会改变帧大小或位置。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你不应该先调用[super viewDidLayoutSubviews]吗?

-(void)viewDidLayoutSubviews{ 
    [super viewDidLayoutSubviews];
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;

    if (screenSize.height <= 480.0f) {
        self.img.frame = CGRectMake(21, 20, 280, 275);
        self.text.frame = CGRectMake(21, 20, 280, 275);
        self.decrease.frame= CGRectMake(55, 308, 39, 39);
        self.add.frame = CGRectMake(55, 308, 39, 39);
        self.increase.frame =CGRectMake(55, 308, 39, 39);
    }
    else{
        self.img.frame = CGRectMake(21, 20, 280, 400);
        self.text.frame = CGRectMake(21, 20, 280, 400);
        self.decrease.frame= CGRectMake(55, 308, 39, 39);
        self.add.frame = CGRectMake(55, 308, 39, 39);
    }    
}