如何将UIViews添加到我的contentView(AwakeFromNib)

时间:2016-09-08 09:40:53

标签: ios objective-c uiview xib awakefromnib

我正在使用从UIView发起的单独awakeFromNib类这是视图层次结构 在我的自定义类中,我必须向contentView添加自定义视图。我没有得到如何将我的customViews作为子视图添加到contentView。

View Hierarchy

这是我的尝试,但我失败了。

 #pragma mark - UINibLoading
-(void)awakeFromNib {
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self loadViewIntoMemory];
[self formUpdateDetailsData];
}

#pragma mark - Private
- (void)loadViewIntoMemory {
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
[self addSubview:contentView];
}

- (void)formUpdateDetailsData {
for (int i = 1; i < 5; i++) {
    inputTextView = [[UIView alloc] initWithFrame:CGRectMake(15, ((i*44)+(i*15)), inputTextView.frame.size.width, 44)];
    [contentView addSubview:inputTextView];
}
for (int i = 5; i < 10; i++) {
    inputPickerView = [[UIView alloc] initWithFrame:CGRectMake(15, ((i*44)+(i*15)), inputPickerView.frame.size.width, 44)];
    [contentView addSubview:inputPickerView];
}
}

2 个答案:

答案 0 :(得分:0)

尝试编写一个初始化方法,该方法将返回您,从.xib文件加载自定义UIView,

- (instancetype)initYourView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

答案 1 :(得分:0)

我理解以下您已创建一个.xib文件,其中包含三个视图1. ContentView 2.输入textview 3.输入选择器视图

现在您想要在内容视图中添加输入视图和输入选择器视图。

现在你可以通过两种方式做到这一点 1.创建输入文本视图的IBOutlet并输入选择器视图,并在内容视图中添加子视图。

  1. 您可以创建视图对象,并在内容视图中添加子视图。

    - (void)loadInputViewInMemory{
    
    UIView *inputView =[[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:nil options:nil]objectAtIndex:1];
    [self addSubview:inputView];
    
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[inputView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:inputView,@"inputView",nil]]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[inputView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:inputView,@"inputView",nil]]];
    
    }
    
  2. 现在使用您在ViewController上创建的自定义视图对象调用此方法。

    示例

    视图中的

    ViewController代码已加载或您要添加自定义视图的位置

     //  You custom view in your case content view.
    CustomView *customView = [[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil]objectAtIndex:0];
    [self.view addSubview:customView];
    customView.translatesAutoresizingMaskIntoConstraints = false;
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[customView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:customView,@"customView",nil]]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:0 views:[NSDictionary dictionaryWithObjectsAndKeys:customView,@"customView",nil]]];
    
    //Call Methods from here instead of calling from awake from nib
    
    
    [customView loadInputViewInMemory];
    

    您可以调用自定义选取器视图添加方法 你需要 将选择器视图的索引更改为2

    像,

    UIView *inputView =[[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:nil options:nil]objectAtIndex:2];
    

    希望这对你有所帮助。