简体   繁体   English

以编程方式处理旋转UIView

[英]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. 为了使此半透明视图位于所有其他内容之上,我添加了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! 提前致谢!

Set the autoresizingMask, eg, 设置autoresizingMask,例如

- (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, eg, 如果这是一个ivar,其他方法也可以将其删除,例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM