0

I spent hours of my day today just to hide PickerView when my app is loaded, but the problem is not with the PickerView. it's with CGRectMake.

I tried a simple project on Xcode just to show you that CGRectMake doesn't work for me...

here's my storyboard (grey area is UIView component) :

enter image description here

and here's my interface :

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *container;

@end

and here's my implementation file :

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _container.frame = CGRectMake(0, 0, 320, 204);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

it's very basic code, not contaminated with other codes... but still, any numbers given to that CGRectMake doesn't make that grey area move. I've changed X, Y value, but the grey UIView stand still just like what you see on storyboard.

why CGRectMake is not working on my case? where's the problem?

UPDATE : I see autolayout is checked on my inspector box

enter image description here

UPDATE : here's my Connection inspector

enter image description here

Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • Do you have AutoLayout enabled in the Storyboard? – Fogmeister Sep 17 '13 at 13:14
  • Yes I did, please check my update. is that what you mean? – Saint Robson Sep 17 '13 at 13:17
  • CGRectMake is working. I'm sure it set the frame correctly. However for the UIView not to react it probably means some layout is taking place at later time (probably auto layout?). Try overriding layoutSubviews and do nothing inside. – demosten Sep 17 '13 at 13:18
  • 2
    Yes, rule 1 of AutoLayout. You can't change the frame of any views. You have constraints that are holding the view in place. You can try to change the frame but it won't work because the constraints are holding it in place. You can either turn off AutoLayout or update the constraints instead of using `_container.frame = ...` – Fogmeister Sep 17 '13 at 13:18

3 Answers3

11

If you have AutoLayout enabled for the Storyboard then changing the frame of a view won't work.

You can't change frames directly using AutoLayout. You have to change the constraints around them.

If you disable AutoLayout for the Storyboard it will work.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    you're a brilliant man!!! you solve this problem!!! many thanks from me... I LOVE YOU, MAN!!! ;-) – Saint Robson Sep 17 '13 at 13:22
  • 1
    The "real" solution though is to get the behaviour you want WITH AutoLayout :) http://www.raywenderlich.com/store/ios-6-by-tutorials This book is awesome. I learnt all the AutoLayout stuff from it. – Fogmeister Sep 17 '13 at 13:32
1

Make sure that your IBOUTLET connection is joined with the view.

user1673099
  • 3,293
  • 7
  • 26
  • 57
1

CGrectmake working only because you set frame size as CGRectMake(0, 0, 320, 204); as per the size the custom view will be visible inside the main view if you want to hide change x or y co-ordinates to out of bound value like this. Then don forget to uncheck autolayout

container.frame = CGRectMake(200, 0, 320, 204);
Yohan
  • 1,108
  • 9
  • 21