我想创建一种“警报 View ”,例如通知用户他已禁用定位服务等。
由于此“警报 View ”仅出现在特定事件中,因此考虑了两种方法,因此目标是创建类似的东西:
V1:
使用方法:
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
然后我必须为 viewcontroller 中的每个 Assets 设置 zPostion,从而确定在哪里设置“警报 View ”。因为它应该是最重要的。而且我必须在代码中创建完整的 View 。
V2:
是在 Storyboard中创建“警报 View ”加上设置特定约束并在运行时更改约束
编辑:
@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];
}];
}
@结束
@implementation LaaxHomeViewController
-(void)viewDidLoad{
[[OHAlertWindow sharedInstance]showWithMessage:@"test"] ;
}
@end
最佳答案
如果您使用的是导航 Controller ,UINavigationItem
具有用于在导航栏按钮上方显示附加文本的 prompt
属性。
self.navigationItem.prompt = "Location services are disabled"
关于ios - 对特定事件发表看法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451586/