UIScrollView从底视图开始

时间:2014-11-07 10:16:14

标签: ios iphone uiviewcontroller uiscrollview

我是IOS计划的新手。

我有两个垂直视图的UIscrollview,并希望页面在第二个/底部视图上自动加载并向上滚动

这是我的UIScrollView代码:

// Set up ScrollView

    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    NSInteger numberOfViews = 2;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat y = i * self.view.frame.size.height;
        UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height)];
        [scrollview addSubview:pageView];

    scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *numberOfViews);
    scrollview.bounces = NO;

    [self.view addSubview:scrollview];

}

2 个答案:

答案 0 :(得分:1)

在viewDidLoad中使用:

- (void)viewDidLoad {
    [super viewDidLoad];    

    CGPoint contentOffset = CGPointMake(0, self.view.frame.size.height);
    scrollView.contentOffset = contentOffset;
}

并在viewDidAppear中:

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

    CGPoint contentOffset = CGPointZero;
    [scrollView setContentOffset:contentOffset animated:true];
}

答案 1 :(得分:0)

我假设您正在viewDidLoad中创建滚动视图 - 只需将此代码添加到viewDidAppear并设置正确的动画持续时间:

[scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height)];

[UIView animateWithDuration:5.0 animations:^{

    [scrollView setContentOffset:CGPointZero];

}];