4

我有一个容器视图,@property (weak, nonatomic) IBOutlet UIView *containerView;但是当我设置它的框架时它不会移动。这是我正在尝试的方法:

- (void)setFramesForCategories
{
    CGRect frame = _containerView.frame;
    frame.origin.x = 20;
    frame.origin.y = self.view.frame.size.height - 52;
    frame.size.width = 236;
    frame.size.height = 60 * [[_dataDict objectForKey:@"Key"] count];
    _containerView.frame = frame;
}

我知道这是基本的,它以前对我有用,当我移动以编程方式创建的对象时,但现在对我不起作用。

4

2 回答 2

13

我遇到的问题是 Storyboard 中设置的约束覆盖了框架更改。这解释了为什么我对以编程方式添加的对象没有同样的问题。要关闭项目的约束,我所要做的就是转到Storyboard.storyboard文件,然后确保“实用程序”侧边栏已打开并选择“文件检查器”选项卡。然后我在“Interface Builder Document”下向下滚动并取消选择“Use Autolayout”。在此之后,我重新运行该项目以发现以编程方式设置框架确实有效。

于 2012-12-06T16:51:48.807 回答
3

视图的“框架”属性用于布局。如果您在情节提要中布局了容器视图并想在代码中移动它。尝试“转换”属性。如,“self.containerView.transform = CGAffineTransformTranslate(self.containerView.transform, 0.0, 10.0);” => 将容器视图向上移动 10 点 :)

于 2014-11-13T06:16:34.830 回答