Search code examples
iosuiviewrotationuiwindow

Handling rotation UIView programmatically


At the very first start-up, the App handles a housekeeping of a database. it takes a while an, therefore, I'added a translucentview with a spinner and a label telling the user what's going on.

In order to have this translucent view on top of everything else, I've added as a subview of self.view.window. The whole code looks like this:

-(void)housekeepDataBase{

    self.searchOptionsControl.hidden=TRUE;
    self.searchBar.hidden=TRUE;

    UIView *translucentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.window.frame.size.width, self.view.window.frame.size.height)];
    translucentView.backgroundColor=[UIColor blackColor];
    translucentView.alpha=0.65;


    UILabel *activityLabel=[[UILabel alloc]initWithFrame:CGRectMake((self.view.center.x-140), (self.view.frame.size.width*0.75), 280, 20)];
    activityLabel.numberOfLines=1;
    activityLabel.textAlignment=UITextAlignmentCenter;
    activityLabel.font=[UIFont fontWithName:@"Arial" size:20.0];
    activityLabel.text=@"Cleaning up database";
    activityLabel.textColor=[UIColor whiteColor];
    activityLabel.backgroundColor=[UIColor clearColor];


    UILabel *activityLabel2=[[UILabel alloc]initWithFrame:CGRectMake((self.view.center.x-140), (self.view.frame.size.width*0.75+40), 280, 20)];    activityLabel2.numberOfLines=1;
    activityLabel2.textAlignment=UITextAlignmentCenter;
    activityLabel2.font=[UIFont fontWithName:@"Arial" size:16.0];
    activityLabel2.text=@"(It might take a while)";
    activityLabel2.textColor=[UIColor whiteColor];
    activityLabel2.backgroundColor=[UIColor clearColor];



    UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.frame=CGRectMake((self.view.center.x-50), (self.view.frame.size.width*0.50), 100, 37);
    [spinner startAnimating];


    [self.view.window addSubview:translucentView];
    [self.view.window addSubview:spinner];
    [self.view.window addSubview:activityLabel];
    [self.view.window addSubview:activityLabel2];

// dealing with the DB

}

Unfortunatelly it causes these views (view, spinner and labels) not to rotate according to the device orientation.

So, the question is: is there any other way to handle this? either the rotation or adding the views? What I want is to have the translucent view to be on top of the navigationbar at the top of the screen and also on top of the toolbar at the bottom.

Any ideas? Thanks in advance!


Solution

  • Set the autoresizingMask, e.g.,

    - (void)housekeepDataBase
    {
        self.searchOptionsControl.hidden=TRUE;
        self.searchBar.hidden=TRUE;
    
        UIViewAutoresizing controlMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        UIViewAutoresizing viewMask    = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
        UIView *view   = self.navigationController.view;
        CGRect  frame  = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
        CGPoint center = CGPointMake(frame.size.width / 2.0, frame.size.height / 2.0);
    
        _containerView                   = [[UIView alloc] initWithFrame:frame];
        _containerView.autoresizingMask  = viewMask;
    
        UIView *translucentView          = [[UIView alloc] initWithFrame:frame];
        translucentView.backgroundColor  = [UIColor blackColor];
        translucentView.alpha            = 0.65;
        translucentView.autoresizingMask = viewMask;
    
        UILabel *activityLabel           = [[UILabel alloc] initWithFrame:CGRectMake((center.x-140), (frame.size.width*0.75), 280, 20)];
        activityLabel.numberOfLines      = 1;
        activityLabel.textAlignment      = UITextAlignmentCenter;
        activityLabel.font               = [UIFont fontWithName:@"Arial" size:20.0];
        activityLabel.text               = @"Cleaning up database";
        activityLabel.textColor          = [UIColor whiteColor];
        activityLabel.backgroundColor    = [UIColor clearColor];
        activityLabel.autoresizingMask   = controlMask;
    
        UILabel *activityLabel2          = [[UILabel alloc] initWithFrame:CGRectMake((center.x-140), (frame.size.width*0.75+40), 280, 20)];
        activityLabel2.numberOfLines     = 1;
        activityLabel2.textAlignment     = UITextAlignmentCenter;
        activityLabel2.font              = [UIFont fontWithName:@"Arial" size:16.0];
        activityLabel2.text              = @"(It might take a while)";
        activityLabel2.textColor         = [UIColor whiteColor];
        activityLabel2.backgroundColor   = [UIColor clearColor];
        activityLabel2.autoresizingMask  = controlMask;
    
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        spinner.frame                    = CGRectMake((center.x-50), (frame.size.width*0.50), 100, 37);
        spinner.autoresizingMask         = controlMask;
        [spinner startAnimating];
    
        [view addSubview:_containerView];
        [_containerView addSubview:translucentView];
        [_containerView addSubview:spinner];
        [_containerView addSubview:activityLabel];
        [_containerView addSubview:activityLabel2];
    
        // dealing with the DB
    }
    

    And, by putting this all in a container view, when you're done you can remove all of these controls just by removing that one view. And if that's an ivar, other methods can remove it, too, e.g.,

    - (void)removeHousekeepDataBase
    {
        [_containerView removeFromSuperview];
        _containerView = nil;
    }