使用故事板和Push时Xcode Scrollview问题

时间:2013-11-06 17:41:08

标签: ios objective-c uiscrollview xcode5

我对xcode很新,并且在使用scrollview时遇到问题。 scrollview工作得很好......但是当使用故事板和推送组合我有一个问题。首次查看滚动视图时,它可以正常工作,但是当您按下新页面然后返回到具有滚动视图的故事板时,它不再滚动。我认为这与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

1 个答案:

答案 0 :(得分:0)

我怀疑你的问题在这里:

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

当用户滚动时,系统可以反复调用此方法。由于您每次都在设置内容偏移量,因此视图不会移动。您应该只执行一次,然后将self.rememberContentOffset设置为CGPointZero,然后在记住的偏移量为零时避免更改内容偏移量。类似的东西:

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