我正在使用UIScrollView我想要显示图像,但问题是我需要水平显示这些图像,现在它们只是垂直显示,如何从垂直方向更改我的UIScrollView的方向水平
这是我实现UIScrollView的课程:
//
// fastoClass.swift
// AutoLayout(
// Created by Barbatos on 5/14/18.
// Copyright © 2018 Seccion 15. All rights reserved.
//
import UIKit
class fastoClass: UIViewController {
@IBOutlet weak var scrollHorizont: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
var constraints = [NSLayoutConstraint]()
var i = 0
var previousLeft: UIView? = nil
var previousRight: UIView? = nil
let scrollWidth: CGFloat = self.scrollHorizont.frame.size.width / 2.0
let imageColors = [UIColor.green, UIColor.lightGray, UIColor.blue, UIColor.red]
for color in imageColors{
let newImage = UIImageView()
newImage.backgroundColor = color
newImage.translatesAutoresizingMaskIntoConstraints = false
var toView : UIView? = nil
var toAttribute : NSLayoutAttribute?
let isLeft = (i % 2) == 0
if isLeft {
toView = self.scrollHorizont
toAttribute = NSLayoutAttribute.leading
} else {
toView = previousLeft
toAttribute = NSLayoutAttribute.trailing
}
var topView : UIView? = nil
var topAttribute : NSLayoutAttribute?
if i < 2 {
topView = self.scrollHorizont
topAttribute = NSLayoutAttribute.top
} else {
if isLeft {
topView = previousLeft
} else {
topView = previousRight
}
topAttribute = NSLayoutAttribute.bottom
}
let top = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.top,
relatedBy: NSLayoutRelation.equal,
toItem: topView,
attribute: topAttribute!,
multiplier: 1.0,
constant: 0)
let leading = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.leading,
relatedBy: NSLayoutRelation.equal,
toItem: toView,
attribute: toAttribute!,
multiplier: 1.0,
constant: 0)
let width = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.width,
relatedBy: NSLayoutRelation.equal,
toItem: self.scrollHorizont,
attribute: NSLayoutAttribute.width,
multiplier: 0.5,
constant: 0)
let height = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.height,
relatedBy: NSLayoutRelation.equal,
toItem: nil,
attribute: NSLayoutAttribute.notAnAttribute,
multiplier: 1.0,
constant: scrollWidth)
constraints.append(top)
constraints.append(leading)
constraints.append(width)
constraints.append(height)
self.scrollHorizont.addSubview(newImage)
i += 1
if isLeft {
previousLeft = newImage
} else {
previousRight = newImage
}
}
self.scrollHorizont.addConstraints(constraints)
self.scrollHorizont.layoutSubviews()
let contentHeight : CGFloat = scrollWidth * (CGFloat(i) / 2.0)
self.scrollHorizont.contentSize = CGSize(width: self.scrollHorizont.frame.size.width, height: contentHeight)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:1)
你可以试试这个
override func viewDidAppear(_ animated: Bool) {
var constraints = [NSLayoutConstraint]()
var i = 0
var previousLeft: UIView? = nil
let imageColors = [UIColor.green, UIColor.lightGray, UIColor.blue, UIColor.red]
for color in imageColors{
let newImage = UIImageView()
newImage.backgroundColor = color
newImage.translatesAutoresizingMaskIntoConstraints = false
if previousLeft == nil {
previousLeft = scrollHorizont
}
var con:NSLayoutAttribute? = nil
if previousLeft == self.scrollHorizont {
con = NSLayoutAttribute.leading
}
else {
con = NSLayoutAttribute.trailing
}
self.scrollHorizont.addSubview(newImage)
let top = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.top,
relatedBy: NSLayoutRelation.equal,
toItem: previousLeft,
attribute: NSLayoutAttribute.top,
multiplier: 1.0,
constant: 0)
let leading = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.leading,
relatedBy: NSLayoutRelation.equal,
toItem: previousLeft,
attribute: con!,
multiplier: 1.0,
constant: 0)
let width = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.width,
relatedBy: NSLayoutRelation.equal,
toItem: self.scrollHorizont,
attribute: NSLayoutAttribute.width,
multiplier: 1,
constant: 0)
let height = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.height,
relatedBy: NSLayoutRelation.equal,
toItem: self.scrollHorizont,
attribute: NSLayoutAttribute.height,
multiplier: 1.0,
constant: 0)
if i == imageColors.count - 1 {
let tra = NSLayoutConstraint(item: newImage,
attribute: NSLayoutAttribute.trailing,
relatedBy: NSLayoutRelation.equal,
toItem: self.scrollHorizont,
attribute: NSLayoutAttribute.trailing,
multiplier: 1.0,
constant: 0)
constraints.append(tra)
}
constraints.append(top)
constraints.append(leading)
constraints.append(width)
constraints.append(height)
previousLeft = newImage
i += 1
}
self.scrollHorizont.addConstraints(constraints)
}