1.使用xib文件
通过xib的⽅式来创建视图对象
NSBundle *bundle = [NSBundle mainBundle]; //获取当前程序的文件目录
NSArray *arr = [bundle loadNibNamed:@"myView" owner:self options:nil];
UIView *myView = [arr objectAtIndex:0];
2.手动绘制
CGRect viewRect = CGRectMake(0, 0, 100, 100);
UIView *myView = [[UIView alloc] initWithFrame:viewRect];
UIView *myView = [[UIView alloc] initWithFrame:viewRect];
参考:
Unarchives the contents of a nib file located in the receiver's bundle.
- (NSArray *)loadNibNamed:(NSString *)
name
owner:(id)
owner
options:(NSDictionary *)
options
Parameters
name |
The name of the nib file, which need not include the |
owner |
The object to assign as the nib’s File's Owner object. |
options |
A dictionary containing the options to use when opening the nib file. For a list of available keys for this dictionary, see Nib File Loading Options. |