0

Before iOS7, I whatever CGRect I define to a UIView's frame, it was working.

For example:

imageView.frame=CGRectMake(20,20,80,80);

was perfectly leaving 20 x 20 margin at the top left of the screen.

Interestingly if I add another UIView with:

nextView.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+10,20,100,100);

nextView appears near imageView, so It somehow accepts X parameter for the second view but it does not accept the Y coordinate for that view too.

Now, I have to use viewWillLayoutSubviews to arrange all of those coordinates. Otherwise, for example, frame for imageView becomes 0,0,80,80 automatically. (it snaps to top-left)

I am not using any nibs and I also even tried creating UIViewController with custom initWithFrame method to set the CGRect before loadView or viewDidLoad but it doesn't matter.

Is there something that automatically overrides x,y of Views? Like an auto-layout system etc? I don't want to use viewWillLayoutSubviews method to rearrange all of those coordinates.

What is happening here?

Update 1

- (id)initWithFrame:(CGRect) frame
{
    self = [super init];
    if (self) {

        self.frame=frame; //frame is custom variable to hold it until loadViews method

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)loadView{
    UIView *view=[[UIView alloc]initWithFrame:_frame];

    view.backgroundColor=[UIColor blackColor];
    self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 80,80)];
    self.imageView.backgroundColor=[UIColor redColor];

    [view addSubview:_imageView];

    self.textView=[[UITextView alloc]initWithFrame:CGRectMake(self.imageView.frame.origin.x+self.imageView.frame.size.width+10, self.imageView.frame.origin.y, view.frame.size.width-20-(self.imageView.frame.origin.x+self.imageView.frame.size.width+10), view.frame.size.height-self.imageView.frame.origin.y-20)];
    [view addSubview:_textView];

    self.view=view;
}

I tried similar code in initWithFrame and also in viewDidLoad. Result was the same.

Update 2

I have mistakenly written "layoutSubviews" instead of "viewWillLayoutSubviews" in the question, so I fixed it: I am not creating a custom UIView but UIViewController, so it had to be "viewWillLayoutSubviews" in the question.

frankish
  • 6,738
  • 9
  • 49
  • 100

2 Answers2

0

My guess is that you are reversing the status bar as the top left point, but in iOS7 the status is transparent is the top point is the actual top left of the screen.

You can change your coordinates or add this to you view controller :

 if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }

This will reset your top left point to the bottom of the status bar.

Any way, I believe putting your layout code in those methods:

- (void)viewWillLayoutSubviews 
- (void)viewDidLayoutSubviews
shannoga
  • 19,649
  • 20
  • 104
  • 169
  • I tried setEdgesForExtendedLayout but it didn't work. Also, entering even 200px for origin.y does not work. So subtracting statusbar's height etc from 200px, it still should not snap to the top. I am trying to stay away from viewWillLayoutSubviews. That is why I am trying to find the thing that resets my x,y values to zero :-/ – frankish Jan 08 '14 at 12:50
  • What is the frame of your view, in the load view, and what happens if you move this code to viewDidLoad ? – shannoga Jan 08 '14 at 12:51
  • I set view's frame to CGRectMake(0, 0, 320, 200). Moving this code into viewDidLoad does not change anything but If I copy the lines that I set frames for the views into viewWillLayoutSubviews, it works (as expected). – frankish Jan 08 '14 at 12:58
0

whenever a subview is added,a UIScrollView is scrolled,Frame is set for View, layoutSubViews? is called,

Zeeshan
  • 4,194
  • 28
  • 32