iOS(iPad):在UISplitView上显示加载屏幕

时间:2013-01-24 11:31:22

标签: ipad loading uisplitviewcontroller

在我的应用程序中,我正在从服务器中获取数据。使用此数据,我填充一个表视图(MasterViewController),当我选择此表中的一个项目时,它将在DetailViewController中显示详细信息。每次启动应用程序或用户按下刷新按钮时都会加载这些信息。

从我想要的服务器接收数据时,会显示一个包含UIActivityIndi​​catorView的视图和一个标记为“正在加载...”的标签。当我的应用程序以纵向模式启动或刷新时,这可以正常工作。但是当我在横向模式下尝试相同时,我的视图大小是不正确的。

以下是我用来加载视图的代码:

+ (SpinnerView *)loadSpinnerViewIntoView:(UIView *)superView {

    SpinnerView *spinnerView = [[SpinnerView alloc] initWithFrame:superView.bounds];

    if (!spinnerView)
        return nil;

    UIImageView *background = [[UIImageView alloc] initWithImage:[spinnerView addBackground]];
    background.alpha = 0.7;
    [spinnerView addSubview:background];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
    indicator.center = superView.center;
    [spinnerView addSubview:indicator];
    [indicator startAnimating];   

    CGSize maximumLabelSize = CGSizeMake(296, 9999);
    CGSize expectedLabelSize = [[NSString stringWithString:@"Loading..."] sizeWithFont:[UIFont boldSystemFontOfSize:25.0f] constrainedToSize:maximumLabelSize];

    UILabel *loadingLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, expectedLabelSize.width, expectedLabelSize.height)];
    loadingLbl.text = @"Loading...";
    loadingLbl.font = [UIFont boldSystemFontOfSize:25.0f];
    loadingLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    loadingLbl.backgroundColor = [UIColor clearColor];
    loadingLbl.textColor = [UIColor whiteColor];
    loadingLbl.center = CGPointMake(superView.center.x, superView.center.y - indicator.frame.size.height - 25);
    [spinnerView addSubview:loadingLbl];   

    [superView addSubview:spinnerView];

    CATransition *animation = [CATransition animation];
    [animation setType:kCATransitionFade];
    [[superView layer] addAnimation:animation forKey:@"layerAnimation"];

    return spinnerView;
}

当我开始更新数据时,从我的MasterViewController调用它:

spinner = [SpinnerView loadSpinnerViewIntoView:self.splitViewController.view];

SpinnerView获取与视图相同的边界,因为它已加载。我真的不明白为什么它在横向模式下无法正常显示。 我希望我的问题是可以理解的,任何人都可以帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

我最后通过将SinnerView调用更改为:

来完成
+ (SpinnerView *)loadSpinnerViewIntoView:(UIView *)superView withFrame:(CGRect)frame {

    SpinnerView *spinnerView = [[SpinnerView alloc] initWithFrame:frame];

    if (!spinnerView)
        return nil;

    .
    .
    .        

    return spinnerView;
}

并从我的viewDidLoad方法调用一个函数,如下所示:

[self performSelectorOnMainThread:@selector(checkLaunchOrientation:) withObject:nil waitUntilDone:NO];

这是功能:

- (void)checkLaunchOrientation:(id)sender {

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIInterfaceOrientationIsLandscape(orientation))
        spinner = [SpinnerView loadSpinnerViewIntoView:self.splitViewController.view withFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
    else
        spinner = [SpinnerView loadSpinnerViewIntoView:self.splitViewController.view withFrame:self.splitViewController.view.bounds];
}

这是一种解决方法,但它对我有用......