查看错误的高度(与父级相同)

时间:2018-03-17 04:30:45

标签: objective-c

我扩展了UIView类以创建重叠加载屏幕。

FullLoadingView.h

#import <UIKit/UIKit.h>

@interface FullLoadingView : UIView

+ (instancetype)showOnView:(UIView *)view;
+ (void)hideAll:(UIView *)view;

- (void)hide;

@end

FullLoadingView.m

#import "FullLoadingView.h"

@implementation FullLoadingView

+ (instancetype)showOnView:(UIView *)view
{
    FullLoadingView *fullLoadingView = [[self alloc] init];
    [fullLoadingView setFrame:view.bounds];
    [fullLoadingView setBackgroundColor:[UIColor colorWithRed:(250.0/255.0) green:(250.0/255.0) blue:(250.0/255.0) alpha:1.0]];

    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicatorView.center = fullLoadingView.center;
    [activityIndicatorView startAnimating];

    [view addSubview:fullLoadingView];

    return fullLoadingView;
}

- (void)hide
{
    [self removeFromSuperview];
}

+ (void)hideAll:(UIView *)view
{
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            FullLoadingView *fullLoadingView = (FullLoadingView *)subview;
            [fullLoadingView removeFromSuperview];
        }
    }
}

@end

UIViewController代码:

- (void)viewDidLoad {
    FullLoadingView *fullLoadingView = [FullLoadingView showOnView:self.view];
}

问题:UIViewControllerNavigationBarTabBar UIActivityIndicatorView的中心对齐方式不正确时。

PS:self.view的高度正确(NavigationBarTabBar除外),但FullLoadingView始终具有设备的完整高度。

inserir a descrição da imagem aqui

1 个答案:

答案 0 :(得分:2)

您在fullLoadingView中创建了viewDidLoad,但self.view之前的viewDidLayoutSubviews帧不保证对当前设备正确。autoresizingMask

最简单的解决方法可能是使用+ (instancetype)showOnView:(UIView *)view { FullLoadingView *fullLoadingView = [[self alloc] init]; fullLoadingView.frame = view.bounds; fullLoadingView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); fullLoadingView.backgroundColor = [UIColor colorWithRed:(250.0/255.0) green:(250.0/255.0) blue:(250.0/255.0) alpha:1.0]; UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activityIndicatorView.center = fullLoadingView.center; activityIndicatorView.autoresizingMask = ( UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin); [fullLoadingView addSubview:activityIndicatorView]; [activityIndicatorView startAnimating]; [view addSubview:fullLoadingView]; return fullLoadingView; } 来保持帧同步:

awk '{for(n=1;n<=NF;n++)print $n>"File"n}' input.txt