1

I am creating a simple blog reader using iOS 6 for the iPhone. I have a UIViewController with a single view configured in my StoryBoard. I then create a UIScrollView in the viewDidLoad method of the view controller using this code:

scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;

[self.view addSubview:scrollView];

NSDictionary *bindings = NSDictionaryOfVariableBindings(scrollView);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:bindings]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:bindings]];

I then add the first 10 entries to the scrollView, and rely on the viewDidLayoutSubviews method to determine the scrollView contentSize:

CGRect contentRect = CGRectZero;

for (UIView *view in scrollView.subviews) {
    contentRect = CGRectUnion(contentRect, view.frame);
}

[scrollView setContentSize:contentRect.size];

In the scrollViewDidScroll delegate method, I determine when the user is near the end of the current scrollView contents and add an additional 10 entries.

This is the code to add a blog entry to the scrollView:

    label = [[UILabel alloc] init];
    label.text = text;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    label.backgroundColor = [UIColor clearColor];
    label.numberOfLines = 0;

    [scrollView addSubview:label];

    if ( priorView ) {
        bindings = NSDictionaryOfVariableBindings(scrollView, priorView, label);

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:0 views:bindings]];
        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[priorView]-10-[label]" options:0 metrics:0 views:bindings]];
    }
    else {
        bindings = NSDictionaryOfVariableBindings(label);

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:0 views:bindings]];
        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[label]" options:0 metrics:0 views:bindings]];
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        label.preferredMaxLayoutWidth = self.view.bounds.size.width - 10;
    });

    priorView = label;

My issue is that after adding the new entries, the contentOffset of the scrollView is reset to the top of the scrollView. I don't understand why this is occurring or how to stop it from occurring.

I am using Autolayout for the view controller's view, the scrollView, and for all subviews comprising the blog entries added to the scrollView.

I would very much like to continue using Autolayout for the actual blog entries as there will be additional details, such as the person posting and images.

I appreciate any assistance. Thanks.

4

0 回答 0