xcode 有没有办法以编程方式更新帧(Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31057862/
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 07:22:22  来源:igfitidea点击:

Is there a way to programmatically update frames (Swift)

iosxcodeswiftframes

提问by Rachel Harvey

Is it possible to update the frames like in the interface builder but programmatically? Some of my objects get misplaced because of an animation (which I'll want to fix anyway, I guess..) but it brought to mind the aforementioned question

是否可以像在界面构建器中那样但以编程方式更新框架?我的一些对象由于动画而错位(我想无论如何我都想修复它......)但它让我想起了上述问题

Edit: I have done some googling as well as looking on stackoverflow and not found what I'm looking for. I want to update the frames of some buttons back to the constraints I set in the interface builder. Sorry if this is too simple of a question, but I'm just looking for a simple answer: yes or no + line of code or the method to call. it probably won't even end up in my final project; I'm just curious.

编辑:我已经做了一些谷歌搜索以及在 stackoverflow 上查找,但没有找到我要找的东西。我想将一些按钮的框架更新回我在界面构建器中设置的约束。对不起,如果这是一个太简单的问题,但我只是在寻找一个简单的答案:是或否 + 代码行或调用方法。它甚至可能不会出现在我的最终项目中;我只是好奇。

Here's some code, since I guess that would help:

这是一些代码,因为我想这会有所帮助:

@IBAction func optionsButtonPushed(sender: AnyObject) {

    UIView.animateWithDuration(2, animations: {
        var theFrame = self.optionsMenu.frame
        theFrame.size.height += 100
        self.optionsMenu.frame  = theFrame
    })
}

The buttons located in the view I'm rolling in (from a height of zero) disappear because of the animation, I guess, so there's probably a better way than what I'm thinking (sorry this question is so crazy; it's my first one!)

我想,位于我滚动的视图中的按钮(从零高度)由于动画而消失,所以可能有比我想的更好的方法(对不起,这个问题太疯狂了;这是我的第一个一!)

采纳答案by Avijit Nagare

You can disable auto-layout. from storyboard. IF you still want to use AutoLayout then use -viewDidLayoutSubviewsmethod.

您可以禁用自动布局。从故事板。如果您仍然想使用 AutoLayout 然后使用-viewDidLayoutSubviews方法。

self.yourButton.translatesAutoresizingMaskIntoConstraints = YES;

self.yourButton.frame = CGRectMake(16, 133, 130, 130);

回答by Tim

UIViewhas a property called framewhich is of type CGRect. CGRectis a struct.

UIView有一个名为framewhich 类型的属性CGRectCGRect是一个结构。

You can update it by creating a new CGRectand assigning it to the property.

您可以通过创建新的CGRect并将其分配给属性来更新它。

CGRect newFrame = CGRect(x:0, y:0, width:100, height:100)
yourView.frame = newFrame

UIView Documentation

UIView 文档