用xib自定义UIView并在代码中使用--iOS

首先新建一个空的xib文件:

将size改为freedom:

然后在xib中自定义视图(添加自己想要的其它视图):

要写好约束, 创建一个继承uiview的类和他关联,然后就可以调用了.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//获得nib视图数组  
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"HeadView" owner:nil options:nil]; // Find the view among nib contents (not too hard assuming there is only one view in it).
// 得到最后一个view
HeadView *headView = [nibContents lastObject]; headView.frame = CGRectMake(0, 0, 300, 200); //headView.backgroundColor = [UIColor blueColor]; _tableView.tableHeaderView = headView; }
原文地址:https://www.cnblogs.com/mafeng/p/5767725.html