问题
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