gpt4 book ai didi

swift - 在 awakeFromNib() 方法中添加约束

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

我的项目中有两个自定义 View 需要以完全相同的比例缩放。所以我决定为此使用 UIScrollView,它非常适合。

我决定开发一个非常简单的继承自 UIScrollView 的类,并在其中初始化所有 View 结构。这样我就避免了 NIB 文件中的任何构造步骤,并且只需添加一个 View 就可以使用我的类。但是我在添加 contentView 的阶段就遇到了一个问题。

这是我的类(class):

final class PlayerContentView: UIScrollView {
fileprivate var contentView: UIView!

override func awakeFromNib() {
super.awakeFromNib()

self.backgroundColor = .clear
setupScrollProperties()

setupContentView()
}

private func setupScrollProperties()
{
self.translatesAutoresizingMaskIntoConstraints = false
self.minimumZoomScale = 1.0
self.maximumZoomScale = 2.0
self.contentSize = frame.size
self.delegate = self
}

private func setupContentView()
{
contentView = UIView(frame: self.frame)
contentView.backgroundColor = .red
self.addSubview(contentView)

CommonSwiftUtility.setSideConstraints(superview: self, view: contentView)
CommonSwiftUtility.setSizeConstraints(superview: self, view: contentView)
}


func requireToFail(_ gestureRecognizer: UIPanGestureRecognizer) {
self.panGestureRecognizer.require(toFail: gestureRecognizer)
} }

下面是添加约束的方法:

static func setSideConstraints(superview: UIView, view: UIView) {
let topConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1.0, constant: 0.0)

let bottomConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .bottom,
relatedBy: .equal,
toItem: superview,
attribute: .bottom,
multiplier: 1.0, constant: 0.0)

let leadingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .leading,
relatedBy: .equal,
toItem: superview,
attribute: .leading,
multiplier: 1.0, constant: 0.0)

let trailingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .trailing,
relatedBy: .equal,
toItem: superview,
attribute: .trailing,
multiplier: 1.0, constant: 0.0)

superview.addConstraint(topConstraint)
superview.addConstraint(bottomConstraint)
superview.addConstraint(leadingConstraint)
superview.addConstraint(trailingConstraint)
}

static func setSizeConstraints(superview: UIView, view: UIView)
{
let wConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .width,
relatedBy: .equal,
toItem: superview,
attribute: .width,
multiplier: 1.0, constant: 0.0)

let hConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
attribute: .height,
relatedBy: .equal,
toItem: superview,
attribute: .height,
multiplier: 1.0, constant: 0.0)

superview.addConstraint(wConstraint)
superview.addConstraint(hConstraint)
}

如您所见,我将我的 contentView 涂成红色,以便在屏幕上对其进行定义。在显示我的 PlayerContentView 后,我得到了这个:

enter image description herePlayerContentView 在全屏上被拉伸(stretch),所以我希望 contentView 是全尺寸的,但显然不是。有人可以请我引用该问题的解决方案吗?提前致谢!

最佳答案

你能设置吗

contentView.translatesAutoresizingMaskIntoConstraints = false

关于swift - 在 awakeFromNib() 方法中添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55455794/

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