xcode 在 Storyboard 中制作的容器视图不会以编程方式更改框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13736649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:15:26  来源:igfitidea点击:

Container View made in Storyboard will not change frame programmatically

iosxcodeuiviewuiviewcontrollercontainers

提问by Patrick Veilleux

I have a container view, @property (weak, nonatomic) IBOutlet UIView *containerView;, but it won't move when I set its frame. Here is how I am trying:

我有一个容器视图,@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;
}

I know this is basic and it has worked for me before, when I was moving programmatically created objects, but it is not working for me now.

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

回答by Patrick Veilleux

The problem I had was the constraints set up in Storyboard were overriding the frame change. This explains why I wasn't having the same issues with programmatically added objects. All I had to do to turn off constraints for the project was to go to the Storyboard.storyboardfile, then make sure the "Utilities" sidebar is open and select the "File Inspector" tab. Then I scrolled down under "Interface Builder Document" and deselected "Use Autolayout." After this, I reran the the project to find setting the frame programmatically did work.

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

回答by Hanton

The "frame" property of a view is used for layout. If you have layout your container view in the storyboard and wanna move it in code. Try the "transform" property. Such as, "self.containerView.transform = CGAffineTransformTranslate(self.containerView.transform, 0.0, 10.0);" => move up the container view for 10 points :)

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