gpt4 book ai didi

ios - 以编程方式使用 ScrollView

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

我正在尝试开发一个仅以编程方式创建 UI 的应用程序。我想创建一个简单的 View ,它是一个 UIScrollView (能够在键盘出现时 ScrollView )和一个 containerView (UIView),我们可以在其中找到一个按钮。

我使用 PureLayout 来更轻松地设置约束,swift 4,Xcode 9.2 beta

在此 View 的类下方

class SimpleView: UIScrollView {

var containerView: UIView!
var signInButton: UIButton!
var signInLabel: UILabel!

var screenSize: CGSize = CGSize.zero

var shouldSetupConstraints = true

override init(frame: CGRect) {
super.init(frame: frame)

self.screenSize = frame.size

self.containerView = UIView(frame: CGRect.zero)
self.signInButton = UIButton(frame: CGRect.zero)
self.signInLabel = UILabel(frame: CGRect.zero)

self.addSubview(self.containerView)

self.containerView.addSubview(self.signInButton)

self.signInButton.addSubview(self.signInLabel)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func updateConstraints() {
if(shouldSetupConstraints) {

self.layoutSignInButton()
self.layoutSignInLabel()

shouldSetupConstraints = false
}
super.updateConstraints()


}


private func layoutContainerView() {
self.containerView.autoPinEdgesToSuperviewEdges()
self.containerView.backgroundColor = UIColor.yellow
}

private func layoutSignInButton() {
self.signInButton.autoPinEdge(toSuperviewEdge: .right)
self.signInButton.autoPinEdge(toSuperviewEdge: .left)
self.signInButton.autoPinEdge(toSuperviewEdge: .top)
self.signInButton.autoSetDimension(.height, toSize: 55.0)

self.signInButton.backgroundColor = UIColor(hex: "#FD9FA2")
}

private func layoutSignInLabel() {
self.signInLabel.autoPinEdgesToSuperviewEdges()

self.signInLabel.shadowColor = UIColor(hex: "#9A615E")
self.signInLabel.shadowOffset = CGSize(width: 0.0, height: 2)
self.signInLabel.text = NSLocalizedString("SIGN IN", comment: "")
self.signInLabel.textAlignment = .center
self.signInLabel.textColor = UIColor.white
self.signInLabel.font = UIFont.boldSystemFont(ofSize: 15.0)
self.signInLabel.backgroundColor = UIColor.clear
}

}

在嵌入前一个 View 的 UIViewController 子类的代码下面

class SignInViewController: UIViewController {

var simpleView: SimpleView!

override func viewDidLoad() {
super.viewDidLoad()

self.simpleView = SimpleView(frame: self.view.bounds) // with SimpleView(frame: self.view.frame) has the same behaviour
self.view.addSubview(self.simpleView)

self.simpleView.autoPinEdgesToSuperviewEdges()
self.navigationController?.navigationBar.isHidden = true

}
}

不幸的是,结果不是预期的:见下文

我错过了什么?缺少不同点: - 按钮的位置很奇怪(按钮和按钮顶部/左侧之间的空间部分隐藏在屏幕之外) - 容器 View 不可见(backgroundColor = UIColor.yellow 没有效果)

提前谢谢您!

/////////////////////////////编辑////////////////////////////////

下面是使用 UIView 而不是 UIScrollView 的完全相同代码的屏幕截图

Class SimpleView: UIView {

enter image description here

最佳答案

UIScrollView 的内容还必须定义 ScrollView 的 .contentSize`。

我不使用 PureLayout,所以我不知道语法是什么,但是在您的 layoutContainerView() 函数中,您还需要执行以下操作:

self.containerView ... set width dimension to SuperviewWdith

这会将 containerView内容设置为 ScrollView 的宽度,并且应该修复宽度部分。

我假设您将向 containerView 添加元素并设置它们的约束来控制它的高度。

关于ios - 以编程方式使用 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47336466/

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