gpt4 book ai didi

ios - 通过代码添加多个 ImageView

转载 作者:行者123 更新时间:2023-11-30 11:12:17 25 4
gpt4 key购买 nike

我尝试在数组中循环时添加 ImageView ..如下所示:

let v = [1,2,3]
w = self.playgroundimg.frame.width/3 - 48.89/2
h = self.playgroundimg.frame.height/4 - 17

for x in v {
let imageName3 = "lineupcardbg"
let image3 = UIImage(named: imageName3)
let imageView3 = UIImageView(image: image3!)
imageView3.frame = CGRect(x: w, y: h, width: 48.89, height: 70.15)
self.playgroundimg.addSubview(imageView3)
w = w + self.playgroundimg.frame.width/3 - 48.89 - 15
}

这工作正常,但是 ImageView 的尺寸会因设备而异..如下所示:

iPhoneX:

enter image description here

iPhone 7 Plus:

enter image description here

正如你所看到的,中间的那个并不以它下面的那个为中心..如何实现这一点,使中间的在中心,而另外两个在它旁边有一些空间?并在所有尺寸中显示相同?

playgroundimg 约束:

enter image description here

最佳答案

这是一个简单的示例 - 只是为了展示如何使用代码中的约束。

您可以将其粘贴到新的 Playground 页面中并运行它以查看结果。

更改行:

get { return CGSize(width: 400, height: 400) }

调整为不同的尺寸以确保 View 保持居中。

import PlaygroundSupport
import UIKit

class RoundedImageView: UIImageView {

override init(frame: CGRect) {
super.init(frame: frame)
layer.cornerRadius = 8
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

class AlignViewController: UIViewController {

override public var preferredContentSize: CGSize {
get { return CGSize(width: 400, height: 400) }
set { super.preferredContentSize = newValue }
}

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .green

var theImageViews = [UIImageView]()

for _ in 0..<4 {

let imgView = RoundedImageView(frame: CGRect.zero)
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.backgroundColor = .white

view.addSubview(imgView)

// make all views the same size and width
NSLayoutConstraint.activate([
imgView.widthAnchor.constraint(equalToConstant: 48.89),
imgView.heightAnchor.constraint(equalToConstant: 70.15),
])

theImageViews.append(imgView)

}

// for easy identification of which views we're referencing
let leftView = theImageViews[0]
let centerView = theImageViews[1]
let rightView = theImageViews[2]
let bottomView = theImageViews[3]

NSLayoutConstraint.activate([

// "center view" will be centered horizontally, and a little above centered vertically
centerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
centerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -25),

// left view is aligned to top of center view, and 20-pts to the left
leftView.topAnchor.constraint(equalTo: centerView.topAnchor),
leftView.trailingAnchor.constraint(equalTo: centerView.leadingAnchor, constant: -20),

// right view is aligned to top of center view, and 20-pts to the right
rightView.topAnchor.constraint(equalTo: centerView.topAnchor),
rightView.leadingAnchor.constraint(equalTo: centerView.trailingAnchor, constant: 20),

// bottom view is centerX aligned to center view, and 20-pts below
bottomView.topAnchor.constraint(equalTo: centerView.bottomAnchor, constant: 20),
bottomView.centerXAnchor.constraint(equalTo: centerView.centerXAnchor),

])

}

}

let viewController = AlignViewController()

PlaygroundPage.current.liveView = viewController

关于ios - 通过代码添加多个 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52057260/

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