我有一个名为UIView
的自定义DialPad
和xib,我试图在我的故事板中使用此对象,但自定义DialPad不会作为子视图添加按钮
我的自定义DialPad:UIView
@implementation DialPad
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
}
return self;
}
我的视图控制器看起来像这样
#import <UIKit/UIKit.h>
#import "DialPad.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *dial;
@end
#import "ViewController.h"
@interface ViewController ()
{
DialPad *d;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
d = [[DialPad alloc] initWithFrame:self.dial.frame];
[self.dial addSubview:d];
}
@end
但视图周围的边框看起来很奇怪
#import "DialPad.h"
#import <QuartzCore/QuartzCore.h>
@implementation DialPad
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1;
}
@end
答案 0 :(得分:2)
要从xib文件加载视图,您应该这样做:
d = [[[NSBundle mainBundle] loadNibNamed:@"DialPad" owner:nil options:nil]
firstObject];
[self.dial addSubview:d];
边框:尝试在- (void)awakeFromNib