简体   繁体   English

iOS AutoLayout不适用于嵌入ScrollView的视图

[英]iOS AutoLayout not really working for views embedded into ScrollView

I'd like to embed views into each other (in the current case into a scrollview to allow displaying taller content than the screen's size) and use iOS 6.0's AutoLayout feature in order to avoid constant calculation of content size's. 我想将视图彼此嵌入(在当前情况下,将其嵌入到滚动视图中,以允许显示比屏幕大小更高的内容),并使用iOS 6.0的AutoLayout功能,以避免不断计算内容大小。

I have the following ViewController's view, containing a UIScrollView: 我有以下ViewController的视图,其中包含UIScrollView:

在此处输入图片说明

I'd like to display my ChildVC's view in this scrollview: 我想在此滚动视图中显示ChildVC的视图:

Please notice that the label is multiline and contains a 'lot of' text, also auto-layout constraints are defined (escpecially with a greater-than-or-equal to the height property). 请注意,标签是多行的,并且包含“很多”文本,并且还定义了自动布局约束(尤其是高度属性等于或大于)。

在此处输入图片说明

I create the childVC and add it to the main VC's view via the following code: 我通过以下代码创建childVC并将其添加到主VC的视图中:

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

    childVC = [[ChildVC alloc] initWithNibName:@"ChildVC" bundle:nil];    

    self.myScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    childVC.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.myScrollView addSubview:childVC.view];

    NSDictionary *viewsDictionary = @{ @"subView" : childVC.view};

    NSString* constr = [NSString stringWithFormat:@"|-0-[subView(%f)]-0-|", self.myScrollView.frame.size.width];    
    [self.myScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constr options:0 metrics: 0 views:viewsDictionary]];

    [self.myScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[subView]-(>=0)-|" options:0 metrics: 0 views:viewsDictionary]];
}

The constraints for the content-view (which I guess will be the childVC's view) are the only stuff set up in code, because I only want a vertical scrollbar, I want the child-view's content to (horizontally) shrink to be displayed in the scrollviewer. 内容视图的约束(我想这将是childVC的视图)是在代码中设置的唯一内容,因为我只想要一个垂直滚动条,我希望子视图的内容(水平)缩小以在其中显示滚动查看器。

And my output is this: 我的输出是这样的:

在此处输入图片说明

The UI is displayed, shrinked but my label does not resize vertically therefore it does not display the whole text :( UI会显示,缩小,但我的标签不会垂直调整大小,因此不会显示整个文本:(

I tried to set up translatesAutoresizingMaskIntoConstraints to the label here and there without any success. 我试图这里和那里建立对所有标签的translationsAutoresizingMaskIntoConstraints都没有成功。

I'd appreciate any help because I'm struggling with this for days now :/ 我将不胜感激,因为我已经为此努力了几天:/

Personally, I think those constraints and the auto layout feature just don't always work exactly as we want them to. 就我个人而言,我认为这些约束和自动布局功能并非总是能像我们希望的那样完全起作用。 (Especially on different screen sizes). (尤其是在不同的屏幕尺寸上)。

So I normally add a few line of code in the viewDidLoad method to move certain objects around by a few pixels. 因此,我通常在viewDidLoad方法中添加几行代码,以将某些对象移动几像素。 You can also change sizes and so on. 您还可以更改大小等。

Literally a simple movement like this does the trick without the user noticing any animations: 从字面上看,这样的简单动作就可以达到目的,而用户不会注意到任何动画:

object_name.frame = CGRectOffset(object_name.frame, 0, -40.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM