gpt4 book ai didi

ios - 如何使 UIScrollView 水平

转载 作者:行者123 更新时间:2023-11-28 23:58:33 25 4
gpt4 key购买 nike

我正在使用一个 UIScrollView,我想在其中显示图像,但问题是我需要水平显示这些图像,现在它们只垂直显示,我怎样才能改变我的 UIScrollView 的方向,从垂直方向水平方向

这是我的类(class),您在其中实现了 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()


}

}

最佳答案

你可以试试这个

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)

}

关于ios - 如何使 UIScrollView 水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50378136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com