让UIView填写UIScrollview

时间:2016-05-18 10:35:00

标签: ios uiview uiscrollview

我有一个包含两个视图的分页滚动视图,第一个是填充UIScrollview,第二个是保持在中间。我不知道出了什么问题,但它不会填满iPhone6,但它适合iPhone5。以下是我的代码:

查看层次结构

 UIView
        --StackView (Properties -- Axis(Vertical) Alignment(Fill) Distribution(Fill) Spacing(0))
            --UIView (Fixed height. 60)
            --UIView (Main Views Holder)
                --ScrollView(Content Size: Screen Width * 2, Height: Height left between it margin to the MainView and BottomView) and I notice, that the scrollview keeps remaining size 433 on both iPhone5 and 6. And when I check my storyboard, that's the exact same height there
                --UIView(Height: 102, Width: Screen Width)BottomView
 @IBOutlet weak var scrollView: UIScrollView!

var rectanglePhot: UIView!
var squarePhoto: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: scrollView.frame.size.height)
    scrollView.autoresizingMask = .FlexibleWidth
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.backgroundColor = UIColor.greenColor()

    iniiCameraView()
}

func iniiCameraView() {
    rectanglePhot = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height)) <-- This is the problematic one
    //rectanglePhot = UIView()
    let specifyY = self.scrollView.frame.size.height - self.view.frame.size.width
    squarePhoto = UIView(frame: CGRect(x: self.view.frame.size.width, y: specifyY, width: self.view.frame.size.width, height: self.view.frame.size.width))
    scrollView.addSubview(rectanglePhot)
    scrollView.addSubview(squarePhoto)

    rectanglePhot.backgroundColor = UIColor.blueColor()
    squarePhoto.backgroundColor = UIColor.brownColor()

    print("Rect Height ", rectanglePhot.frame.size.height)
    print("Scroll Height ", scrollView.frame.size.height)
    print("Specify Y ", specifyY)
}

图片Image Added to Scrollview a index[0]

3 个答案:

答案 0 :(得分:1)

在viewdidappear中添加代码。自动布局将在viewdidload

之后应用

答案 1 :(得分:0)

替换

UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height))

使用:

UIView(frame: CGRect(x: 0, y: 0, width: self.scrollView.bounds.size.width, height: self.scrollView.bounds.size.height))

有了这个,UIView将采用你的滚动视图的确切大小

答案 2 :(得分:0)

如果您不想使用autolayout:在subview中添加viewDidLoad,这很好,但您还需要在{{{{}}中调整frame s 1}} viewDidLayoutSubviews的方法,因为UIViewControllerscrollView通过后会有不同的框架。

如果您可以使用autolayout:不要依赖autolayout来调整scrollView的大小,将subview的大小与subview的大小相关联{1}}