0

I want create a UIView that's offscreen when ViewDidLoad is called, but that I will animate up onto the screen once a certain function has been called. The code for animating the UIView is fine, but I can't seem to draw the UIView offscreen to begin with (I've assembled the UIView in storyboard onto my UIViewControleller). This is the code I've tried:

- (void)viewDidLoad {

    ...

     [_tagView setFrame:CGRectMake(0, self.view.frame.size.height+40, self.view.frame.size.width, 40)];

    ...

}

_tagView is drawn where I've placed it in the StoryBoard editor, which is at the bottom of my UIViewController.

Apollo
  • 8,874
  • 32
  • 104
  • 192

1 Answers1

1

A couple of things: A view object's frame gives it's coordinates in it's parent view's coordinate space.

A view's bounds are it's internal coordinate system.

When you set a view's frame, it should be expressed in terms of it's parent's bounds, not it's parent's frame.

So your code should be creating your _tagView's frame in terms of self.view.bounds, not self.view.frame.

Next, are you using AutoLayout or struts and springs? In Xcode 5 everything defaults to AutoLayout. You can't change frames and bounds when AutoLayout is active. Instead you have to change the relevant constraints. If you want to set a view's frame manually, you probably want to turn off AutoLayout and use "struts and springs" style view geometry.

EDITED: I moved some of the info from comments up into my original answer for clarity:

If you want to move a view at runtime with Auto Layout active, you have to find the constraint for the setting you want to change, (width, height, leading edge, trailing edge, etc.) and control-drag from the constraint into your .h file to make an outlet. Then in your code you change the constant property associated with the constraint. – Duncan C 22 mins ago

Note that you CAN turn Auto Layout off on a storyboard-by-storyboard or NIB-by-NIB basis. If you select the file and choose the File Inspector, there is a checkbox "Use Autolayout" hidden in the "Interface Builder Document" section. I always have trouble finding it.

Here is a screen-shot showing the location of the button in the Xcode project window (far right side)enter image description here

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks that's very useful information. I'm using the lastest verison of Xcode (5.0.2) so I am using AutoLayout. Would I want to set the views y coor in storyboard to be the views frame+40 then? Also, evne though I'm using AutoLayout some of my subviews don't draw properly on iPhone 4's. Is this a result of hardcoding values into IB instead of using bound? – Apollo Feb 15 '14 at 04:43
  • AutoLayout is very confusing. In AutoLayout, the placement of views in IB actually means almost nothing. At runtime, the system looks at the constraints on your views and moves/resizes them based on those constraints. If you try to change a view's frame, the constraints often change it back, with no visible effect. You want to set up your constraints in IB, then go to the Editor menu, pick "Resolve Auto Layout Issues", and "Update Frames". That will move your views around to show where they should be based on your current constraints. – Duncan C Feb 15 '14 at 14:51
  • If you want to move a view at runtime with Auto Layout active, you have to find the constraint for the setting you want to change, (width, height, leading edge, trailing edge, etc.) and control-drag from the constraint into your .h file to make an outlet. Then in your code you change the constant property associated with the constraint. – Duncan C Feb 15 '14 at 15:08
  • Note that you CAN turn Auto Layout off on a storyboard-by-storyboard or NIB-by-NIB basis. If you select the file and choose the File Inspector, there is a checkbox "Use Autolayout" hidden in the "Interface Builder Document" section. I always have trouble finding it. Let me know if you can't find it and I'll post a link to a screen-shot. – Duncan C Feb 15 '14 at 15:13