UIScrollview Autolayout Issue

2019-01-13 20:29发布

I have a problem with autolayout(maybe) and my scrollview!

My Problem

  1. I scroll down View

2.Then I push to another View

3.Then I go back and the scrollview looks like that and I'm not able to scroll to the highest point.(I see it in the bouncing of the scrollview) Scrollview

Can anybody help me?

9条回答
虎瘦雄心在
2楼-- · 2019-01-13 20:49

try this

@property (nonatomic, assign) CGPoint scrollViewContentOffsetChange;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scrollView.contentOffset = CGPointZero;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.scrollViewContentOffsetChange = _scrollView.contentOffset;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _scrollView.contentOffset = self.scrollViewContentOffsetChange;
}
查看更多
老娘就宠你
3楼-- · 2019-01-13 20:52

The issue cause ScrollView was set ContentOffset before AutoLayout applied. the solution is:

Create private property

@property (assign,nonatomic) CGPoint scrollviewContentOffsetChange;

Add code to view method

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.scrollView.contentOffset = CGPointZero;
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  self.scrollviewContentOffsetChange = self.scrollView.contentOffset;
}

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];

  self.scrollView.contentOffset = self.scrollviewContentOffsetChange;
}
查看更多
叼着烟拽天下
4楼-- · 2019-01-13 20:53

The following code snippet in the containing view controller also seems to solve the problem, without relying on explicit sizes:

- (void)viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];
  self.mainScrollView.contentOffset = CGPointZero;
}

It does reset the content offset to the origin, but it seems that so do the other answers.

查看更多
【Aperson】
5楼-- · 2019-01-13 20:53

I had the same problem. Turned out I was setting a constraint on the content view aligning the it's y-center to the superview's y-center. When I deleted this constraint it worked just fine.

查看更多
地球回转人心会变
6楼-- · 2019-01-13 20:54

I was using adam's solution, but started to have problems when i was dismissing with animated:YES. In my code, content offset gets set a while after viewWillAppear (as viewWillAppear appears to be too soon).

- (void)viewDidDisappear:(BOOL)animated
{
    self.scrollOffsetToPersist = self.scrollView.contentOffset;
    self.scrollView.contentOffset = CGPointZero;

    [super viewDidDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^
     {
         self.scrollView.contentOffset = self.scrollOffsetToPersist;
     }];
}

EDIT: another, better way is to reset it back in viewDidLayoutSubviews :)

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if(!CGPointEqualToPoint(CGPointZero, self.scrollOffsetToPersist))
    {
        self.scrollView.contentOffset = self.scrollOffsetToPersist;
        self.scrollOffsetToPersist = CGPointZero;
    }
}
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-13 20:59

This isn't great but I beat auto-layout (definitely not the correct way but I was sick of trying!) by setting the content size in viewDidAppear after autolayout happens, setting the scrollOffset and persisting the scroll offset in viewDidDisappear, and then setting the scroll offset back to it's persisted state in viewDidAppear.

Like this:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    self.scrollView.contentSize = self.scrollViewInnerView.frame.size;
    self.scrollView.contentOffset = [self.scrollOffsetToPersist CGPointValue];

}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
    self.scrollOffsetToPersist = [NSValue valueWithCGPoint:self.scrollView.contentOffset];
    self.scrollView.contentOffset = CGPointZero;
}

Not at all elegant, but works so thought I'd share.

查看更多
登录 后发表回答