以编程方式处理旋转UIView

时间:2012-07-03 20:07:26

标签: ios uiview rotation uiwindow

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

为了将这个半透明的视图放在其他所有内容之上,我添加了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

}

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

所以,问题是:还有其他方法可以解决这个问题吗?轮换还是添加视图? 我想要的是让半透明视图位于屏幕顶部导航栏的顶部,也位于底部工具栏的顶部。

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分: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;
}