TK189
TK189

Reputation: 1500

Create view in load view and set it's frame, but frame auto changes

Why the frame of the view auto changed?

see this codes:

- (void)loadView{
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(-10,0,340,480)];
    self.view = sv;
    NSLog(@"sv frame = %@", NSStringFromCGRect(self.view))
}

- (void)viewWillAppear{
  NSLog(@"view frame = %@", NSStringFromCGRect(self.view));
}

In my demo Output : sv frame = {{-10, 0}, {340, 480}}

view frame = {{0, 0}, {320, 480}}

It should be: view frame = {{-10, 0}, {340, 480}}

But in PhotoScroller demo (2010 WWDC 104), it's correct. It's so strange.

Upvotes: 1

Views: 707

Answers (3)

wattson12
wattson12

Reputation: 11174

- (void)loadView
{    
    UISCrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = scrollView;
}

Upvotes: 1

javieralog
javieralog

Reputation: 1337

try this:

- (void)loadView{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(-10,0,340,480)];
    [view addSubview:sv];
    sv.autoresizingMak=UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingMaskFlexibleHeight;
    self.view = view;
}

Upvotes: -1

graver
graver

Reputation: 15213

Change both of the methods this way:

- (void)loadView{
   UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectZero];
   self.view = sv;
}

- (void)viewWillAppear:(BOOL)animated {
   self.view.frame = CGRectMake(-10,0,340,480);
}

EDIT: Geometry MUST be set in at least viewWillAppear: and later on UIViewController's event lifecycle. Autoresizing masks also can change the geometry.

Upvotes: 2

Related Questions