在addSubview

时间:2018-10-10 14:15:45

标签: uiscrollview autolayout contentsize

我在xib中创建了一个高度为800的customView:

customView with 800 height

我想通过addSubview调用它,如UIViewController所示,进入蓝色视图:

wantToShowHereView in UIViewController

这里的问题是我希望我的UIScrollView可以动态设置它的contentSize,因为在这里我将它的高度定义为800:

enter image description here

我从另一篇文章中读到,AutoLayout中的contentSize将由其subView的高度定义,在本例中为contentView。但在将高度设置为800后,滚动仍未到达视图的底部(粉红色)。怎么设置自动布局呢?

Broken ScrollView

这是我在customView中的代码:

- (id)initWithFrame:(CGRect)frame
{
NSLog(@"initWithFrame");
self = [super initWithFrame:frame];
if (self) {
    [self setup];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"initWithCoder");
self = [super initWithCoder:aDecoder];
if(self) {
    [self setup];
}
return self;
}

- (void)setup {

[[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil];
[self addSubview:self.view];
NSLog(@"contentSize Height :%f", self.myscrollview.contentSize.height);
NSLog(@"contentView Height :%f", self.contentView.frame.size.height);
}

在我的ViewController.m中,我这样称呼它:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
[self.wantToShowHereView addSubview:customV];

}

有人可以指导我如何实现它吗? 这是我为您准备的示例项目:AutoLayoutScrollView Example

1 个答案:

答案 0 :(得分:0)

问题。

首先,将customV的帧设置为wantToShowHereView的边界-但是您要在viewDidLoad()中这样做。在viewDidLoad()和您在屏幕上实际看到它的时间(设备大小,方向等)之间,界限几乎肯定会改变。

第二,customVUIView,您要将XIB的“根视图”(包含滚动视图)添加为customV的子视图,但是...不要在视图上设置任何约束(或其他调整大小的行为)。

第三,您将相对约束与绝对约束(宽度,高度,前导,尾随等)混合在一起,当整体框架发生变化时,这又会引起问题... ,您是在明确设置customV的框架,而不是在运行时添加约束。

您可以开始解决问题:

第一步-从customV中删除viewDidLoad()实例。

第二步-添加以下viewDidAppear()方法。

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

    customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
    customV.view.frame = customV.bounds;
    [self.wantToShowHereView addSubview:customV];

}

只需执行 即可为您提供正确的滚动视图。

可能要做的是将初始化保留在viewDidAppear()中,但在其中添加约束以使用自动布局。

此外,我建议重新处理customView.xib中元素的约束,以便滚动(contentSize)由滚动视图的实际内容决定,而不是硬编码高度为您的contentView


编辑:

viewDidLoad(在ViewController.m中)的外观如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    customView *customV = [[customView alloc] initWithFrame:self.wantToShowHereView.bounds];
    [self.wantToShowHereView addSubview:customV];

    customV.translatesAutoresizingMaskIntoConstraints = NO;

    [customV.topAnchor constraintEqualToAnchor:self.wantToShowHereView.topAnchor].active = YES;
    [customV.bottomAnchor constraintEqualToAnchor:self.wantToShowHereView.bottomAnchor].active = YES;
    [customV.leadingAnchor constraintEqualToAnchor:self.wantToShowHereView.leadingAnchor].active = YES;
    [customV.trailingAnchor constraintEqualToAnchor:self.wantToShowHereView.trailingAnchor].active = YES;

}

setup中的customView.m

- (void)setup {

    [[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil];
    [self addSubview:self.view];

    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
    [self.view.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
    [self.view.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
    [self.view.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;

    NSLog(@"contentSize Height :%f", self.myscrollview.contentSize.height);
    NSLog(@"contentView Height :%f", self.contentView.frame.size.height);

}
相关问题