3

I am currently using iOS 6.0.

I have a custom UIView that needs to have a certain size. If I programmatically init the view and add it it's fine. However, I can't find a place where I can set the size of the view in the storyboard.

Setting its size in the storyboard doesn't work because the storyboard thinks it's empty and set it's size to zero. Setting its size in viewDidLoad or viewDidAppear doesn't work because later on the size will be overwritten by _applyISEngineLayoutValue.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
user1724886
  • 31
  • 1
  • 2

3 Answers3

1

You can do that in your Interface Builder. Open the storyboard where you have your view and open the utilities menu:

enter image description here

Then you can select a button that looks like a ruler on the top of the utilities menu:

enter image description here

In that menu you can set the size of your view and how you want it to expand.

Also, please make sure you setted your Class' view in the class inspector:

enter image description here

Image token from this site.

Finally, make sure you override the initWithFrame and initWithCoder methods:

- (id)initWithFrame:(CGRect)frame {
    return [super initWithFrame:frame];
}

//Needs to be overrided when you set your size in interface builder
- (id)initWithCoder:(NSCoder *)aDecoder {
    return [self initWithFrame:[self frame]];
}
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • As I said, no matter what I set in the inspector, the size will be set to zero. – user1724886 Oct 06 '12 at 10:59
  • Did you set your class in the inspector as I have shown in my edit? – Tiago Almeida Oct 06 '12 at 11:04
  • So, I have a MyView:UIView with drawRect method, say paint a circle. I then put a UIView in the root view using storyboard, set the class to MyView, and set the size to 800 * 600. What I expect is a circle on the screen, but there is nothing because the size of the view is zero. – user1724886 Oct 06 '12 at 11:28
  • Just added two more things you have to make sure you made. Please check if you overrided `initWithFrame` and `initWithCoder` methods. – Tiago Almeida Oct 06 '12 at 11:57
  • 2
    Storyboard won't give proper size during initialization anyway, sizes are set later. – user1724886 Oct 06 '12 at 13:41
  • 1
    You should definitely not call both initWithCoder: and initWithFrame: on a single instance of UIView. Each object should only pass through a designated initializer once, and that code runs a single instance through multiple designated initializers. – Jon Hess Oct 16 '12 at 00:32
  • @JonHess you are right, didn't realize that mistake (next time feel free to edit my post :). I believe that calling the `initWithFrame` is enough? – Tiago Almeida Oct 16 '12 at 09:50
1

I am facing the same problem, and I think the short answer is: you can't rely on the frame or bounds in the view's init method when using storyboards.

When your view is initialized by the segue, it will call the initWithCoder: method rather than other init methods, since it is deserializing your view object from the .xib file. Before storyboards, the frame and bounds would be set by the time the initWithCoder: was called, but that appears to no longer be the case with storyboards -- the iOS code sets those values later.

I've used a couple workarounds, depending on the situation:

  1. If I know the size of the view in advance (for example a specialty view that only supports one size) I set my own frame in the initWithCoder: method.

  2. If I don't know the size of the view, I defer initialization of size-specific things until my view's layoutSubviews method is called.

  3. If it's more convenient, I sometimes just explicitly do the size-specific initialization in my view's ViewController. (Not pretty, but sometimes quick-and-easy. I'm not proud.)

AndrewCr
  • 549
  • 9
  • 18
  • I think you've hit the nail on the head, Andrew - it "plain doesn't work" in a storyboard milieu. Apple seems to set it very late. – Fattie Mar 30 '14 at 16:34
0

I have the same problem as you, and when I select the view and switch to the attributes inspector , setting "Simulated Metrics" as follows, I can resize the view in the Size inspector.

Make sure that Size is set to either "Freeform" or "None"

Simulated Metrics

winux
  • 452
  • 4
  • 12
jianpx
  • 3,190
  • 1
  • 30
  • 26