zoukankan      html  css  js  c++  java
  • iOS 开发自定义一个提示框

    在开发的时候,会碰到很多需要提示的地方,提示的方法也有很多种,ios 8 以前的版本有alertview还是以后用的alertController,都是这种作用,

    但是不够灵活,而且用的多了,用户体验也不好,所以很简单的,我们自定义一个label,来当做提示与用,好了,闲话少说,直接上代码:

    1、property一个label

    @property(nonatomic,strong)UIView *hudView;

    2、写一个方法,返回这个view

    -(UIView *)hudView
    {
        if (_hudView == nil) {
            
            UIView *hudView = [[UIView alloc]init];
            hudView.frame = [UIApplication sharedApplication].keyWindow.bounds;
            UILabel *label = [[UILabel alloc]init];
            label.frame = CGRectMake(0, 0, 300, 30);
            label.textAlignment = NSTextAlignmentCenter;
    //        CGPoint center = hudView.center;
    //        center.x = center.x + 50; //这里可以调整x轴上的坐标,当然了y轴的的也可以调
            label.center = hudView.center;
            label.font = [UIFont systemFontOfSize:20];
            label.textColor = [UIColor orangeColor];
            label.text = @"<-向左滑动 向右滑动->";
            hudView.hidden = YES;
            [hudView addSubview:label];
            [[UIApplication sharedApplication].keyWindow addSubview:(_hudView = hudView)];
            
        }
        
        
        return _hudView;
    }

    3、调用,在什么时候显示,什么时候隐藏

    -(void)viewWillAppear:(BOOL)animated
    {
        self.hudView.hidden = NO;
    }
    
    -(void)viewDidAppear:(BOOL)animated
    {
        self.hudView.hidden = YES;
    }

    注:这里介绍下view加载的时候走的四个方法

    viewDidLoad-加载视图
    
    viewWillAppear-UIViewController     对象的视图即将加入窗口时调用;
    
    viewDidApper-UIViewController       对象的视图已经加入到窗口时调用;
     
    viewWillDisappear-UIViewController  对象的视图即将消失、被覆盖或是隐藏时调用;
    
    viewDidDisappear-UIViewController   对象的视图已经消失、被覆盖或是隐藏时调用;

    效果图如下:

    在view已经加载到窗口时,让label隐藏

     大家可以单独写一个工具类,就是方法,这样就可以随心所欲的加载了,

  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/hero11223/p/5709928.html
Copyright © 2011-2022 走看看