Search code examples
iosuiviewios9statusbar

when home button pressed view origin changed?


I have this code in viewDidLoad:

[self.navigationController setNavigationBarHidden:YES];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.view.backgroundColor = [UIColor whiteColor];


    //we need to add white background behind the status bar
    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    UIView* statusBarWhiteBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, statusBarHeight)];
    [statusBarWhiteBackGround setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:statusBarWhiteBackGround];

    UIView * redBackGroundColor  = [[UIView alloc] init];
    [redBackGroundColor setBackgroundColor:[UIColor redColor]];
    redBackGroundColor.frame =CGRectMake(0,statusBarHeight , self.view.frame.size.width, self.view.frame.size.height-statusBarHeight);
    [self.view addSubview:redBackGroundColor];

statusWhiteBackGround view is used to change the color of the status bar. here is the result of the above code:

enter image description here

but when the app enter background and then back to foreground, the redBackGroundColor view change it's orging.y as you can see in the bellow picture:

enter image description here

what's the problem ?thanks


Solution

  • Create a UIView programatically on method

    -(void)viewDidLayoutSubviews{
    
         screen=[[UIScreen mainScreen] bounds];
    
    //Simple view
        self.customView = [[CustomAlertOKView alloc]initWithFrame:CGRectMake(0, 20, screen.size.width, screen.size.height)];
    
         [self.view addSubView:customView];
    
    }
    

    or

    Create a view using storyboard and set the x as 0, y value as 20,width and height based on the screen size

    @IBOutlet UIView *customView;
    

    set the outlet in the storyboard,

    Then set the background color of the custom view to redcolor and main view to white color

    self.view.backgroundColor = [UIColor whiteColor];
    customView.backgroundColor = [UIColor redColor];