以编程方式创建scrollview-contentview-views,不显示contentview

时间:2016-11-04 20:40:48

标签: ios swift uiscrollview

我正在尝试以编程方式创建一个滚动视图,允许用户在将手机切换到横向模式时垂直滚动,但到目前为止我没有运气:((我试图以多种方式完成这项工作,但它只是没有工作,搜索根本没有帮助)

我想要的: 我想创建一个scrollview,它允许我将滚动视图内的视图保持在视图中的良好位置,而不会相互堆叠。

重大尝试次数

第一次尝试: 我创建了一个滚动视图,其中锚点附加到superview的(self.view)各自的锚点,我添加了一个自定义按钮,其顶部,左侧,右侧锚点附加到滚动视图的相应锚点(顶部) , 左右)。然后我实现了viewWillTransitionToSize方法来更改scrollView的内容大小,以确保scrollview保持屏幕较长边的高度。滚动按预期工作,但滚动视图的子视图未正确显示。我想这可能是因为我们不鼓励我们在同一个滚动视图中添加多个视图(有人可以解释为什么会这样吗?)。

第二次尝试: 我在scrollview中创建了contentview,以包含我可能想要添加到scrollview的所有视图,但是仍未显示此内容视图。

以下是我到目前为止编写的代码

let scrollView: UIScrollView = {
    let scrollView = UIScrollView()
    scrollView.contentSize = UIScreen.mainScreen().bounds.size
    scrollView.backgroundColor = UIColor.blueColor()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    return scrollView
}()
let contentView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.yellowColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()
let loginButton: UIButton = {
    let button = UIButton()
    button.backgroundColor = UIColor.blackColor()
    button.setTitle("LOGIN", forState: .Normal)
    button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button.layer.cornerRadius = 5
    button.layer.masksToBounds = true
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    // To portrait
    if(size.width < size.height){
        self.scrollView.contentSize = CGSize(width: size.width, height: size.height)
    }// To Landscape
    else{
        self.scrollView.contentSize = CGSize(width: size.width, height: size.width)
    }
}
func setupViews(){
    self.view.addSubview(self.scrollView)
    self.scrollView.addSubview(self.contentView)
    self.contentView.addSubview(self.loginButton)
}
func setupConstraints(){
    self.scrollView.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
    self.scrollView.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
    self.scrollView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
    self.scrollView.rightAnchor.constraintEqualToAnchor(self.view.rightAnchor).active = true

    self.contentView.topAnchor.constraintEqualToAnchor(self.scrollView.topAnchor).active = true
    self.contentView.bottomAnchor.constraintEqualToAnchor(self.scrollView.bottomAnchor).active = true
    self.contentView.leftAnchor.constraintEqualToAnchor(self.scrollView.leftAnchor).active = true
    self.contentView.rightAnchor.constraintEqualToAnchor(self.scrollView.rightAnchor).active = true

    self.loginButton.bottomAnchor.constraintEqualToAnchor(self.contentView.bottomAnchor).active = true
    self.loginButton.leftAnchor.constraintEqualToAnchor(self.contentView.leftAnchor).active = true
    self.loginButton.rightAnchor.constraintEqualToAnchor(self.contentView.rightAnchor).active = true
    self.loginButton.heightAnchor.constraintEqualToConstant(50).active = true

}
override func viewDidLoad() {
    super.viewDidLoad()

    self.setupViews()
    self.setupConstraints()
}
编辑:这是我使用上面的代码运行模拟器时手机屏幕的屏幕截图。 Description here

蓝屏是滚动视图,目前黄色的contentview没有出现在滚动视图的顶部。

0 个答案:

没有答案
相关问题