0

我对 xcode 还很陌生,并且遇到了滚动视图的问题。滚动视图工作正常......但是当使用情节提要和推送组合时我遇到了问题。第一次查看滚动视图时它工作正常,但是当您推送到新页面然后返回故事板时滚动视图不再滚动。我认为这与 didload 有关,也许我应该使用 willappear?代码如下。我认为我需要以某种方式更改此代码,以便每次返回此故事板时都会重新加载故事板...有其他人遇到此问题或建议如何解决吗?

//
//  PrintViewController.m
//
//

#import "PrintViewController.h"

@interface PrintViewController ()

@end

@implementation PrintViewController
@synthesize PrintScroller, rememberContentOffset;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [PrintScroller setScrollEnabled:YES];

    // Do any additional setup after loading the view.
}

- (void) viewDidAppear:(BOOL)animated {
    [PrintScroller setContentSize:CGSizeMake(300, 595)];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.PrintScroller.contentOffset = CGPointMake(0, 0);
}

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

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

0

我怀疑你的问题在这里:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}

当用户滚动时,系统可能会重复调用此方法。由于您每次都设置内容偏移量,因此视图不会移动。您应该只执行一次,然后设置self.rememberContentOffsetCGPointZero,然后如果记住的偏移量为零,则避免更改内容偏移量。就像是:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.rememberContentOffset.y > 0) {
        self.PrintScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
        self.rememberContentOffset = CGPointZero;
    }
}
于 2013-11-06T17:51:13.067 回答