ios - 以编程方式调整大小/对齐加载 View

标签 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 的子类,具有白色背景、微调器 (UIActivityIndi​​catorView) 和标签:“Loading ……”。现在我想将这两个东西放在中心并使它们有点“坚如磐石”,所以当你在 viewDidLoad 上初始化那个 View 然后它的 superview 由于导航栏标签栏 一切都很好地居中。哦,我想让它处理旋转……等等。

下面是我尝试过的方法:

#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;
}

因此标签和微调器被设置为其他 View 的 subview ,并且该 View 在我们的 LoadingView 中居中并且所有的支柱和 Spring 都未设置,因此在调整大小后它应该留在中间。 LoadingView 设置了所有的支柱和 Spring ,因此它会随着它的 superview 一起改变大小......

但这一切都没有按预期工作,我觉得我走错了路,我的代码是错误的。谁能帮我解释一下我应该看什么?

最佳答案

好像是

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

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

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

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

关于ios - 以编程方式调整大小/对齐加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278302/

相关文章:

ios - 是否有可能在NSMutableArray中混淆addObject :?

ios - 如何更改输入总线上的抽头频率?

ios - 在 cellForItemAtIndexPath 中的 UICollectionViewCell 中更改 UIView 的框架

iphone - 将工具栏添加到 UITableViewController

xcode - 接管一个 Xcode iOS 项目 : code sign error

ios - 在 OCUnit 应用程序测试中绕过登录/身份验证

ios - 以核心文本为中心绘制 NSAttributedString

ios - NSJSONSerialization 解析错误 - 无法识别的选择器

objective-c - 使用 String 创建一个 128 位的 UUID

iOS : How to save selected row of tableview even I refresh tableview?