0

我正在使用单独UIView的类,该类awakeFromNib从这是我的自定义类的视图层次结构,我必须在其中添加自定义视图contentView。我不知道如何将我的 customViews 作为子视图添加到 contentView。

查看层次结构

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

 #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];
}
}
4

2 回答 2

0

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

- (instancetype)initYourView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}
于 2016-09-08T10:00:09.417 回答
0

我了解以下内容 您已经创建了一个 .xib 文件,其中包含三个视图 1. ContentView 2. input textview 3. Input picker view

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

现在您可以通过两种方式执行此操作 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]]];
    
    }
    

现在使用您在 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];

希望这对您有所帮助。

于 2016-09-08T11:12:55.223 回答