如何将我的UIScrollView向下移动,以便在ScrollView外部的视图顶部显示图片?

时间:2016-02-10 21:44:51

标签: ios objective-c uiscrollview

我正在尝试使用UIScrollView来创建在视图控制器中向下移动的scrollView。我正在使用xib文件来创建scrollview,并希望将滚动视图添加到我的视图中,移动到视图屏幕的一部分。

以下是我用来添加scrollview

的代码
- (void)viewDidLoad {

    [super viewDidLoad];
    [self setLabelsAndPicture];
    self.scrollView.contentSize = self.scrollView.frame.size;

    // then set frame to be the size of the view's frame
    self.scrollView.frame = self.view.frame;

    // now add our scroll view to the main view
    [self.view addSubview:self.scrollView];
    // Do any additional setup after loading the view from its nib.
}

1 个答案:

答案 0 :(得分:0)

计算您的起始y点和新高度以适应imageView 请注意,您应该使用自动布局

- (void)viewDidLoad {

    [super viewDidLoad];
    [self setLabelsAndPicture];
    self.scrollView.contentSize = self.scrollView.frame.size;

    // then set frame to be the size of the view's frame
    self.scrollView.frame = CGRectMake(0,YourImageView.bounds.size.Height, 
                                       self.view.bounds.size.width, 
                                       self.view.bounds.size.height - 
                                       YourImageView.bounds.size.Height );
    // <<< calculate your starting y point and new height to accommodate for the imageView

    // now add our scroll view to the main view
    [self.view addSubview:self.scrollView];
    // Do any additional setup after loading the view from its nib.
}