0

在第一次启动时,该应用程序处理数据库的内务管理。这需要一段时间,因此,我添加了一个带有微调器的半透明视图和一个告诉用户发生了什么的标签。

为了将这个半透明视图置于其他所有内容之上,我已将其添加为 self.view.window 的子视图。整个代码如下所示:

-(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

}

不幸的是,它会导致这些视图(视图、微调器和标签)不根据设备方向旋转。

所以,问题是:有没有其他方法可以处理这个问题?旋转或添加视图?我想要的是让半透明视图位于屏幕顶部的导航栏顶部,以及底部工具栏的顶部。

有任何想法吗?提前致谢!

4

1 回答 1

3

设置 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
}

而且,通过将这一切都放在一个容器视图中,当您完成后,您只需移除一个视图即可移除所有这些控件。如果这是一个 ivar,其他方法也可以删除它,例如,

- (void)removeHousekeepDataBase
{
    [_containerView removeFromSuperview];
    _containerView = nil;
}
于 2012-07-03T20:35:39.120 回答