ios提示框,自动消失

时间:2024-11-11 08:35:02

-(void)click

{

NSString *showMsg = @"点击我,开始提示";

int width = showMsg.length * 20;

UIWindow *window = [UIApplication sharedApplication].keyWindow;

UIView *tipView = [[UIView alloc]init];

tipView.frame = CGRectMake((window.frame.size.width/2)-width/2, (window.frame.size.height/2)-30, width, 50);

tipView.backgroundColor = [UIColor blackColor];

UILabel *labelView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, 50)];

labelView.text = showMsg;

labelView.textColor = [UIColor whiteColor];

labelView.font = [UIFont systemFontOfSize:15];

labelView.textAlignment = kCTCenterTextAlignment;

[tipView addSubview:labelView];

[window addSubview:tipView];

[UIView animateWithDuration:2 animations:^{tipView.alpha = 0.0;} completion:^(BOOL finished){[tipView removeFromSuperview];}];

}