将自定义UIView和xib添加到storyboard中

时间:2015-08-21 02:41:09

标签: objective-c uiview uistoryboard xib

我有一个名为UIView的自定义DialPad和xib,我试图在我的故事板中使用此对象,但自定义DialPad不会作为子视图添加按钮

我的自定义DialPad:UIView

@implementation DialPad


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

我的自定义xib名为DialPad enter image description here

我的视图控制器看起来像这样

#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

和我的故事板 enter image description here

但视图周围的边框看起来很奇怪

enter image description here

#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

enter image description here

1 个答案:

答案 0 :(得分:2)

要从xib文件加载视图,您应该这样做:

d = [[[NSBundle mainBundle] loadNibNamed:@"DialPad" owner:nil options:nil]
                 firstObject];
[self.dial addSubview:d];

边框:尝试在- (void)awakeFromNib

中添加边框代码