以编程方式调整大小/对齐加载视图

时间:2012-04-23 09:54:11

标签: ios objective-c cocoa-touch loading uiactivityindicatorview

好的,我正在向UITableViewController添加一个加载屏幕,就像在YouTube,AppStore或iTunes应用程序中一样。而且我这样做:

[self.view addSubview:[[LoadingView alloc] initWithFrame:self.view.bounds]];
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
dispatch_async(downloadQueue, ^{
    //load data
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self.view.subviews lastObject] removeFromSuperview]; 
        //reload data graphicaly
    });
});
dispatch_release(downloadQueue);

LoadingViewUIView的子类,其背景为白色背景(UIActivityIndicatorView)和标签:“正在加载... ”。现在我想把这两件事放在中心并使它们有点“坚如磐石”,所以当你在viewDidLoad上启动那个视图然后它的 superview 由于< em>导航栏和标签栏一切都保持良好的中心位置。哦,我希望它能够处理轮换......等等。

现在我正试图这样做:

#define LABEL_WIDTH 80

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                     0,
                                                                     spinner.frame.size.width + 5 + LABEL_WIDTH,
                                                                     spinner.frame.size.height)];
        innerView.center = self.center;
        CGRect transitionFrame = spinner.frame;
        transitionFrame.origin.x = innerView.frame.origin.x;
        transitionFrame.origin.y = innerView.frame.origin.y;
        spinner.frame = transitionFrame;
        transitionFrame.origin.x = transitionFrame.origin.x+5+spinner.frame.size.width;
        transitionFrame.size.width = LABEL_WIDTH;
        UILabel *label = [[UILabel alloc] initWithFrame:transitionFrame];
        label.text = @"Loading…";
        [innerView addSubview: spinner];
        [spinner startAnimating];
        [innerView addSubview: label];
        innerView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                      UIViewAutoresizingFlexibleRightMargin |
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin);
        [self addSubview:innerView];
        self.autoresizingMask = (UIViewAutoresizingFlexibleWidth|
                                 UIViewAutoresizingFlexibleHeight);
    }
    return self;
}

因此标签和微调器被设置为其他视图的子视图,并且该视图在我们的LoadingView中居中并且未设置所有支柱和弹簧,因此在调整大小后它应保持在中间。并且LoadingView设置了所有struts和spring,因此它将与它的 superview 一起改变大小......

但这一切都没有达到预期的效果,而且我觉得自己处在糟糕的道路上并且我的代码错了。有人可以帮我一点,并解释一下我应该看看什么?

1 个答案:

答案 0 :(得分:0)

似乎

innerView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                              UIViewAutoresizingFlexibleRightMargin |
                              UIViewAutoresizingFlexibleTopMargin |
                              UIViewAutoresizingFlexibleBottomMargin);

以内心观为中心是个坏主意。

我通过重新定义layoutSubviews方法解决了这个问题。

代码本身可以在https://github.com/Uko/UILoadingView/blob/master/UILoadingView.m

找到