提供特定事件的视图

时间:2016-02-17 08:38:29

标签: ios objective-c

我想创建一种“警报视图”,例如,他已禁用的用户是位置服务等。

由于这种“警报视图”仅出现在特定事件中,并且想到了两种方法,因此目标是创建类似于此的东西: enter image description here

V1:

使用方法:

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

然后我必须为viewPtroller中的每个资源设置zPostion,以确定在何处设置“警报视图”。因为它应该是最重要的。我将不得不在代码中创建视图完成。

V2:

是在故事板中创建“警报视图”还是设置特定约束并在运行时更改约束

编辑:

@implementation OHAlertWindow
// Initialize the UIWindow subclass and put a UILabel inside it
- (id)init {
    if(self = [super init]) {
        CGRect navigationBarFrame;
        CGRect statusBarFrame;
        UILabel *messageLabel;

        statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        navigationBarFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.origin.y,
                                        statusBarFrame.size.width, statusBarFrame.size.height + 50);
        messageLabel = [[UILabel alloc] init];
        [messageLabel setTextColor:[UIColor whiteColor]];
        [messageLabel setTextAlignment:NSTextAlignmentCenter];
        [self setWindowLevel:UIWindowLevelAlert]; // THIS IS IMPORTANT!!
        [self setAlpha:0.0f];
        [self setHidden:NO];
        [self setFrame:statusBarFrame];
        [self addSubview:messageLabel];
    }
    return self;
}

// Use singleton pattern so we only need one instance of the class
// throughout the whole app
+ (OHAlertWindow *)sharedInstance {

    static OHAlertWindow *_sharedInstance = nil;

    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[OHAlertWindow alloc] init];
    });

    return _sharedInstance;
}
- (void)showWithMessage:(NSString *)message {
    [UIView animateWithDuration:1.f animations:^{
        [self setAlpha:1.0f];
    }];
}

@end

@implementation LaaxHomeViewController
-(void)viewDidLoad{
    [[OHAlertWindow sharedInstance]showWithMessage:@"test"] ;
}
@end

2 个答案:

答案 0 :(得分:1)

如果您使用的是导航控制器,则UINavigationItem具有prompt属性,用于在导航栏按钮上方显示其他文字。

    self.navigationItem.prompt = "Location services are disabled"

enter image description here

答案 1 :(得分:0)

我需要一个项目的类似功能。以下是我解决它的方法。

我的解决方案是继承UIWindow。以下是实施中最重要的部分:

@implementation OHAlertWindow

...

// Initialize the UIWindow subclass and put a UILabel inside it
- (id)init {
    if(self = [super init]) {
        statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        navigationBarFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.origin.y,
                                        statusBarFrame.size.width, statusBarFrame.size.height + kNavBarHeight);
        _messageLabel = [[UILabel alloc] init];
        [_messageLabel setTextColor:[UIColor whiteColor]];
        [_messageLabel setTextAlignment:NSTextAlignmentCenter];
        [self setWindowLevel:UIWindowLevelAlert]; // THIS IS IMPORTANT!!
        [self setAlpha:0.0f];
        [self setHidden:NO];
        [self setFrame:statusBarFrame];
        [self addSubview:_messageLabel];
    }
    return self;
}

// Use singleton pattern so we only need one instance of the class
// throughout the whole app
+ (OHAlertWindow *)sharedInstance {

    static OHAlertWindow *_sharedInstance = nil;

    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[OHAlertWindow alloc] init];
    });

    return _sharedInstance;
}
...

@end

然后在我的应用程序中,我调用[[OHAlertWindow sharedInstance] showWithMessage:@"hello World!"];,这将使消息显示在导航栏上方(我实现了show方法以淡化窗口的alpha值以获得良好的效果)。

如果您有任何疑问,请与我联系!

修改

以下是显示/隐藏方法:

- (void)showWithMessage:(NSString *)message {
    if(_isShowing) return;
    [UIView animateWithDuration:kFadeInAnimDur animations:^{
        [self setAlpha:1.0f];
    } completion:^(BOOL finished) {
        [NSTimer scheduledTimerWithTimeInterval:kWaitDur
                                         target:self
                                       selector:@selector(hide)
                                       userInfo:nil repeats:NO];
    }];
    [self setIsShowing:YES];
}

- (void)hide {
    [UIView animateWithDuration:kFadeOutAnimDur animations:^{
        [self setAlpha:0.0f];
    }];
    [self setIsShowing:NO];
}