在UIScrollView中设置大图像

时间:2015-07-04 14:48:22

标签: ios swift uiscrollview uiimageview

我想将图像放在UIScrollView中。但问题是,当我尝试将图像放大到UIScrollView时,UIScrollView会显示图像的上半部分。我希望scrollview显示图像的底部。现在,我有以下代码:

let imgBot1 = UIImage(named:"Img1.jpg");
let imgBot2 = UIImage(named:"Img4.jpg");
let imgBot3 = UIImage(named:"Img5.jpg");

//Adding UIImage in UIImageView
let imgView1 = UIImageView(image:imgBot1)
let imgView2 = UIImageView(image:imgBot2)
let imgView3 = UIImageView(image:imgBot3)     

//creating UIScrollView
let scrView2 = UIScrollView()

scrView2.frame = CGRectMake(0, self.view.frame.height/2,
self.view.frame.width, self.view.frame.height)

//content size of the scrollview
scrView2.contentSize = CGSizeMake(self.view.frame.width*3, self.view.frame.height)

//Size and place of UIImageView
imgView1.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
imgView2.frame = CGRectMake(width, 0, self.view.frame.width, self.view.frame.height)
imgView3.frame = CGRectMake(width*2, 0, self.view.frame.width, self.view.frame.height)


//adding the scrollview to the view
self.view.addSubview(scrView2)

scrView2.addSubview(imgView1)
scrView2.addSubview(imgView2)
scrView2.addSubview(imgView3)

scrView2.pagingEnabled = true


//Initial location of the scrollview
scrView2.contentOffset = CGPointMake(0, 0);

我想知道如何让scrollview显示图像的底部,而不是顶部。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我制作了这段代码,希望它对你有用。我使用了故事板,制作了一个scrollView,添加了一个imageView并为图像视图添加了约束以粘贴边缘(不知道它是必要的)。

imageView = UIImage(named: "image")
scrollView.contentOffset = CGPoint(x: 0, y: image.image!.size.height)