这是我正在使用的代码:

-(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 中的代码不起作用。它不会在加载 View 时改变帧大小或位置。有什么建议么?

最佳答案

你不应该先调用 [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);
    }
}

关于objective-c - CGRectMake 不适用于 viewDidLayoutSubviews 中的自定义框架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15886435/

10-09 16:28