如何为我的ScrollView修复AutoLayout?

时间:2016-05-05 22:05:39

标签: swift uiscrollview ios9 xcode7 ios-autolayout

我有一个视图控制器,它有一个UIView(ParentView),然后是UIScrollview(ScrollView)。 ParentView锚定到ViewController的前导,尾随,顶部和底部。 ScrollView锚定到ParentView的前导,尾随,顶部和底部。结构是这样的:

-UIView
--ParentView
---ScrollView

然后我创建了一个包含图像和标签的xib文件。 xib将动态添加到ScrollView。 xib框架的高度和宽度等于ScrollView的高度和宽度。

当我在iphone 6上运行模拟器时,滚动效果很好; xib的宽度正好是屏幕的宽度。但是当我用iphone 6运行模拟器时,xib的宽度与屏幕大小相同,滚动到下一个项目时会有一个额外的空间。我该怎么做才能删除它!?

这是iphone 6 plus的屏幕截图

enter image description here

这是iphone 6的屏幕截图。注意第二个屏幕的额外空间 enter image description here

这是我的代码:

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ParentView: UIView!
@IBOutlet weak var ScrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let photos = ["joe", "john"]
    let userNames = ["joe", "john"]
    self.ScrollView.frame = CGRectMake(0, 0, self.ParentView.frame.size.width, self.ParentView.frame.size.height)

    self.ScrollView.contentSize = CGSizeMake(self.ParentView.frame.size.width * CGFloat(photos.count), self.ParentView.frame.size.height)
    self.ScrollView.pagingEnabled = true
    var i: Int = 0
    while i < 2 {
        if let userXib = NSBundle.mainBundle().loadNibNamed("User", owner: self, options: nil)[0] as? User {
            userXib.frame = CGRect(x: self.ScrollView.frame.size.width * CGFloat(i), y: 0, width: self.ScrollView.frame.size.width, height: userXib.frame.size.height)
            userXib.assignValues(photos[i], myName: userNames[i])
            self.ScrollView.addSubview(userXib)
        }
        i = i + 1
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

更新#1 - Coder1000解决方案: 我尝试实施#3&amp;通过在滚动内添加UIView图层,在答案中为#4。我称之为ScrollMainView。在故事板中,我设置其前导,尾随,顶部和底部以填充其超级视图:Scrollview然后在循环中将这些元素添加到它而不是scrollview。 但是现在滚动不会滚动!我的代码和最终显示如下所示:

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ParentView: UIView!
@IBOutlet weak var ScrollView: UIScrollView!
@IBOutlet weak var ScrollMainView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    let photos = ["joe", "john"]
    let userNames = ["joe", "john"]
    self.ScrollView.frame = CGRectMake(0, 0, self.ParentView.frame.size.width, self.ParentView.frame.size.height)
    self.ScrollView.contentSize = CGSizeMake(self.ParentView.frame.size.width * CGFloat(photos.count), self.ParentView.frame.size.height)
    self.ScrollView.pagingEnabled = true
    self.ScrollMainView.frame = CGRectMake(0, 0, self.ScrollView.frame.size.width, self.ScrollView.frame.size.height)
    print("the frame for parentview is: \(self.ParentView.frame)")
    var i: Int = 0
    while i < 2 {
        if let userXib = NSBundle.mainBundle().loadNibNamed("User", owner: self, options: nil)[0] as? User {
            userXib.frame = CGRect(x: self.ScrollMainView.frame.size.width * CGFloat(i), y: 0, width: self.ScrollMainView.frame.size.width, height: userXib.frame.size.height)
            userXib.assignValues(photos[i], myName: userNames[i])
            self.ScrollMainView.addSubview(userXib)
        }
        i = i + 1
    }
    self.ScrollView.addSubview(ScrollMainView)
      }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

enter image description here

1 个答案:

答案 0 :(得分:2)

有几种解决方案,在viewDidLoad中框架设置不正确,所以当你问它时,它是错误的。

稍后会通过autolayout约束调整parentView和scrollView,但是用户视图不是......

一个简单的解决方案可能是将代码放入:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.ScrollView.frame = CGRectMake(0, 0, self.ParentView.frame.size.width, self.ParentView.frame.size.height)

    self.ScrollView.contentSize = CGSizeMake(self.ParentView.frame.size.width * CGFloat(photos.count), self.ParentView.frame.size.height)
    self.ScrollView.pagingEnabled = true

    for subView: UIView in self.ScrollView.subviews {
        if subView is User { subView.removeFromSuperview()
    }

    var i: Int = 0
    while i < 2 {
        if let userXib = NSBundle.mainBundle().loadNibNamed("User", owner: self, options: nil)[0] as? User {
            userXib.frame = CGRect(x: self.ScrollView.frame.size.width * CGFloat(i), y: 0, width: self.ScrollView.frame.size.width, height: userXib.frame.size.height)
            userXib.assignValues(photos[i], myName: userNames[i])
            self.ScrollView.addSubview(userXib)
        }
        i = i + 1
    }
}    

或者您可以通过编程方式为自定义xib添加约束。

或者你可以保留一组你的costom xib并调整它们的帧,以及在viewDidLayoutSubviews()

中的SCROLLVIEW CONTENTSIZE

或者您可以将代码放在viewDidAppear()