Is there a way to programmatically update frames (Swift)

妖精的绣舞 提交于 2019-12-08 17:19:21

问题


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.

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!)


回答1:


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

self.yourButton.translatesAutoresizingMaskIntoConstraints = YES;

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



回答2:


UIView has a property called frame which is of type CGRect. CGRect is a struct.

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

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

UIView Documentation



来源:https://stackoverflow.com/questions/31057862/is-there-a-way-to-programmatically-update-frames-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!