Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to programmatically update frames (Swift)

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

like image 888
Rachel Harvey Avatar asked Jun 25 '15 18:06

Rachel Harvey


2 Answers

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);
like image 148
Avijit Nagare Avatar answered Sep 18 '22 18:09

Avijit Nagare


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

like image 26
Tim Avatar answered Sep 20 '22 18:09

Tim