在iOS中实现类似安卓自动消失提示框

时间:2021-09-17 03:44:04

类方法:

+ (void)showMessage:(NSString *)message {
// 获取window
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *showView = [[UIView alloc] init];
showView.backgroundColor = [UIColor blackColor];
showView.frame = CGRectMake(, , , );
showView.alpha = 1.0f;
showView.layer.cornerRadius = 5.0f;
showView.layer.masksToBounds = YES;
[window addSubview:showView]; UILabel *label = [[UILabel alloc] init];
UIFont *font = [UIFont systemFontOfSize:];
CGRect labelRect = [message boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, ) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
label.frame = CGRectMake(, , ceil(CGRectGetWidth(labelRect)), CGRectGetHeight(labelRect));
label.text = message;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[showView addSubview:label];
showView.frame = CGRectMake((SCREEN_WIDTH - CGRectGetWidth(labelRect) - )/, , CGRectGetWidth(labelRect)+, CGRectGetHeight(labelRect)+);
[UIView animateWithDuration:1.5 animations:^{
showView.alpha = ;
} completion:^(BOOL finished) {
[showView removeFromSuperview];
}]; }