0

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!

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Marcal
  • 1,371
  • 5
  • 19
  • 37

1 Answers1

3

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;
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I did try already, but doesn't work. Thanks for the thougth though. – Marcal Jul 03 '12 at 20:39
  • @Marcal If you add your views to self.view instead of self.view.window, I think you will find it will handle rotation events fine. I put your code in my app as a test with that change (`self.view`) and it works for me. I assume your rotation is otherwise working fine, and that it is just a question of placement of these controls and their automatic adjustment on reorientation. – Rob Jul 03 '12 at 21:09
  • thanks for the explanation. The problem I see if I add the views to self.view is that the translucent view does not cover the whole screen. It misses the navigationBar, for instance. – Marcal Jul 03 '12 at 21:26
  • @Marcal How about `self.navigationController.view`? That works for me, covering the nav bar, too. – Rob Jul 03 '12 at 21:33
  • Wow, exactly what I was looking for. Good call! Now works nice (I've to tune up the autoresizingMask though). I wasn't happy adding subview directly to the self.view.window anyway. Thanks a lot! – Marcal Jul 03 '12 at 21:42
  • @Marcal The translucentView should be `UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight`. The labels and spinner should be `UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin` – Rob Jul 03 '12 at 21:55
  • @Marcal Tweaked answer with concept of container view (so you could remove all of these controls just by removing one view). – Rob Jul 03 '12 at 22:03
  • I gets weird when you start the App in landscape mode. The translucent view only goes as fas as aprox 75% of the screen width (from left to the right). – Marcal Jul 03 '12 at 22:31
  • @Marcal Are you using `UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight` for the translucentView? You want it to scale with the window (whereas the labels and spinner you want the margins to scale, hence `UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin`). – Rob Jul 04 '12 at 00:43
  • Yes, I actually copy-pasted your code. It looks like a split-view but the master is on the rigth side. – Marcal Jul 04 '12 at 18:37
  • @Marcal Not sure if I understand what you're describing. This is iPhone, right? or iPad? And are you seeing the problem landscape or portrait? Perhaps you can also provide screen snapshot. – Rob Jul 04 '12 at 21:02
  • it is iPad and the problem only appears when the App is started from Landscape mode. Weird, uh? – Marcal Jul 04 '12 at 21:11
  • @Marcal You can try `self.splitViewController.view` instead of `self.navigationController.view`. – Rob Jul 04 '12 at 21:14
  • yeah, I thougth about that, but it doesn't work because I don't have any splitViewController. It doesn't matter, I think I will leave it as non-rotable screen. It is a one-time-thing anyway. Only happens the very first time the App launches. – Marcal Jul 04 '12 at 21:18