·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> CustomUIView通过xib实现一个简单地自定义视图

CustomUIView通过xib实现一个简单地自定义视图

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23
1 /**
2  *  使用Custom UIView的这条路,我走的很不顺利,前期犯2,中期有事,浪费了很多时间!
3  *  现在空闲的时间不多了,只能抓紧时间写两个小demo,一来做练习,二来整理下也许能给要学习的人一些帮助!
4  */
1 /**
2  *  Custom UIView常用的三种实现方法:
3  *  Method_1:通过初始化的方式(eg:UITableViewCell的重写)
4     Method_2:通过drawRect的方法,使用代码自己画出想要的视图
5     Method_3:通过xib
6  */

 

 

 

 

 

/**
 *  初始化代码:
 *
 *  @return [nibView objectAtIndex:0]
 */
+ (ZYCustomView *)initCustomView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"ZYCustomView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
/**
 *  在ViewController里引用
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    ZYCustomView *test = [ZYCustomView initCustomView];
    test.backgroundColor = [UIColor yellowColor];
    test.clipsToBounds = YES;
    test.frame = CGRectMake(0, 20, 320, 200);
    test.lb1.text = @"我是第一行";
    [self.view addSubview:test];
}