CGRectMake is not working with UIView

2019-01-15 22:46发布

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

3条回答
小情绪 Triste *
2楼-- · 2019-01-15 23:24

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

查看更多
Anthone
3楼-- · 2019-01-15 23:26

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);
查看更多
相关推荐>>
4楼-- · 2019-01-15 23:30

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.

查看更多
登录 后发表回答