CGRectMake is not working with UIView

2019-01-15 22:55发布

问题:

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) :

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

UPDATE : here's my Connection inspector

回答1:

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.



回答2:

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



回答3:

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);