Ios 如何将自定义UIView从xib文件加载到UIViewController中的UIView?

Ios 如何将自定义UIView从xib文件加载到UIViewController中的UIView?,ios,objective-c,uiview,uiviewcontroller,storyboard,Ios,Objective C,Uiview,Uiviewcontroller,Storyboard,我创建了一个xib文件。 代码如下: #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

我创建了一个xib文件。

代码如下:

#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。在ViewWillDisplay中,我正在尝试将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];
}
@property(nonatomic, weak) IBOutlet UIView *containerView;
但是,xib文件超出了我命名为containerView的已定义UIView。我已将containerView链接到ViewController中的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];
}
@property(nonatomic, weak) IBOutlet UIView *containerView;
有人能帮我为什么得到这个输出吗?我需要在ViewController中的containerView中获取自定义uiview。像文本字段和按钮一样,应该在黑色边框内


在容器视图中添加视图时,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)];

希望这有帮助

现在,自定义视图位于已定义的容器视图中。但它并不符合这一观点。如何完美地将自定义视图放入容器视图>能否给出一个屏幕截图,它现在是如何显示的。容器视图正在扩展,容器视图和登录视图的宽度、高度不一样吗?你使用过自动布局吗?是的,但是自定义视图并不完全适合容器视图。