2016-06-29 107 views
1

我创建了一个xib文件。 enter image description here如何将自定义UIView从xib文件加载到UIViewController中的UIView中?

下面是代码:

#import "LoginView.h" 
@implementation LoginView 

-(id)initWithFrame:(CGRect)frame{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // Initialization code 
    [self setup]; 
} 
return self; 
} 

-(void) setup{ 
[[NSBundle mainBundle] loadNibNamed:@"LoginView" owner:self options:nil]; 

self.bounds = self.loginView.bounds; 

[self addSubview:self.loginView]; 
} 

现在,我创建了一个视图控制器和一个UIView添加到它。在viewWillAppear中,我试图将xib文件加载到我的视图控制器中定义的UIView中。

-(void)viewWillAppear:(BOOL)animated{ 
self.containerView.layer.borderColor = [UIColor blackColor].CGColor; 
self.containerView.layer.borderWidth = 2.0f; 
LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(self.containerView.frame.origin.x,self.containerView.frame.origin.y, self.containerView.frame.size.width, self.containerView.frame.size.height)]; 
[self.containerView addSubview:loginView]; 
} 

但是,在厦门国际银行文件是要定义的UIView,我叫containerView之外。我已经将容器视图链接到ViewController中的UIView。

@property(nonatomic, weak) IBOutlet UIView *containerView; 

谁能帮我为什么我收到这个输出?我需要在ViewController的containerView中获得自定义的uiview。像文本框和按钮应该在黑色边框内。

enter image description here

回答

1

当您添加您的容器视图查看,则x和y应该是0,0

您需要更换此:此

LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(self.containerView.frame.origin.x,self.containerView.frame.origin.y, self.containerView.frame.size.width, self.containerView.frame.size.height)]; 

LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(0,0, self.containerView.frame.size.width, self.containerView.frame.size.height)]; 

希望这有助于!

+0

现在,自定义视图位于定义的容器视图内。但它不符合这个观点。如何完美地适应容器视图中的自定义视图> – bthapa

+0

你可以给出一个截图,它现在如何显示。 –

+0

http://screenshot.net/rr04zfm, – bthapa