5

I have an Objective-C project I'm refactoring and I've enountered a line that I can't translate and Apple's documentation site seems to be down:

[self.stepperView setFrame:self.stepperView.frame];

I've tried this:

    self.stepperView.frame = stepperView.frame

but the view is still getting drawn in the wrong spot. Does anyone know the setFrame equivalent in Swift?

I tried looking at Apple's documentation, but it seems the site is down:

enter image description here

2 Answers 2

8

setFrame and frame are equivalent in Objective-C and Swift.

In Objective-C, setFrame is the generated setter method for a property. You can also do (in Objective-C) view.frame = someFrame which is 100% the same as calling setFrame: it's just a matter of which syntax you prefer.

In Swift, all the setVariable: method names are replaced with the dot-syntax equivalent of view.frame = ....

As far as why your view is getting drawn in the wrong spot, you'd have to post a concrete example of how you're placing your view. It's most likely a math or calculation error. It's also worth noting that the iOS coordinate layout has y-values increasing as you go down, unlike typical Cartesian coordinates.

I also noticed that in one of the tabs in your screenshot that you searched "unable to set frame before viewDidLoad". viewDidLoad is the wrong place to be setting any frame values. According to the documentation:

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

which means viewDidLoad is the place for initialization of state, variables, whatever but not the place for layout because it's incredibly likely that somewhere in between viewDidLoad and viewDidAppear that something else, something out of your control is re-positioning your view. The most likely culprit is the view bounds changing and adjusting all subviews.

3
  • Awesome explanation. I powered down for the evening, but I'll give this a whirl in the morning and let you know how it goes. Thank you for such a detailed response. I think you ID'd the culprit (viewDidLoad)
    – Adrian
    Nov 18, 2015 at 4:21
  • 1
    Sure thing. Also if you want documentation on hand, check this out: kapeli.com/dash
    – barndog
    Nov 18, 2015 at 4:24
  • 1
    Thank you for the Dash recommendation. That is a sweet app!
    – Adrian
    Nov 19, 2015 at 4:10
0

If you are using storyboards with Auto Layout and have on Outlet connected to your view, setting the view.frame will not work unless you set translatesAutoresizingMaskIntoConstraints = true for the object you are trying to resize or just uncheck Auto Layout in the File Inspector of Xcode

If you do not use Auto Layout then you can set the view.frame size in viewDidLoad() and it will work

If you are adding subviews programmatically and setting the view.frame size, at runtime then Auto Layout will not interfere with that views frame size. Although if you add your new view as a subview of one defined in the storyboard and clipToBounds is true or Clips Subviews is checked, your new view will be clipped.

I believe this is the case for both Swift and Objective-C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.