Search code examples
iosobjective-cios7uiviewaddsubview

Adding a subview to view changes other subviews' frames


A uiviewcontroller has two UIViews and one tableview added in stroyboard ib and connected with IBoutlet

when user click swipe in top uiview, I call following method to change frames of all subview IBoutlet elements. That works.

-(void)hideTopViews
{
    [UIView animateWithDuration:0.25 animations:^{
        [_overallHealth setHidden:YES];
        _overallHealth.frame =  CGRectMake(0, 0,self.view.frame.size.width,0);
        [_healthBar setHidden:YES];
        _sortView.frame =  CGRectMake(0, 45,self.view.frame.size.width,_sortView.frame.size.height);
        _portfoList.frame =  CGRectMake(0, _sortView.frame.origin.x+_sortView.bounds.size.height+50,self.view.frame.size.width,self.view.frame.size.height);
        _sortButton.frame =  CGRectMake(90, 45,_sortButton.frame.size.width,_sortButton.frame.size.height);
        [self.view bringSubviewToFront:_sortButton];
        self.navigationItem.leftBarButtonItems=nil;
    }];


}

However when I add another subview programmatically to self.view all subviews goes back to their original positions like in storyboard.

-(void)showPortfolioDetailsScreen
{
    PortfolioDetails *portView=[[PortfolioDetails alloc] initWithFrame:CGRectMake(300,200,200,200)];
    portView.backgroundColor=[UIColor redColor];
    [self.view addSubview:portView];

}

enter image description here

enter image description here

enter image description here

How can I fix this?


Solution

  • @tanzolone is on right track, if you are using only portrait mode and want your storyboard to stop changes your subviews to original frames go to your storyboard and uncheck Use Autolayout

    so you can use the old struts-and-springs model.

    here is a detailed tutorial of autolayouts though http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2