gpt4 book ai didi

swift - 在 UIScrollView 中看不到按钮

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:47 24 4
gpt4 key购买 nike

我在 swift 中以 ScrollView 的形式制作了一个列表,其中 View 由各种类型组成,例如标签、按钮等。

然而,当我将按钮添加到 subview 时,尽管显示了所有其他标签等,但它们并未显示。我还尝试在约束和 anchor 中搞乱。另一方面,当我将相同的按钮添加到 self.view.addsubview 而不是 scrollview.addsubview 时,它们只是不滚动显示,因为不再是 ScrollView 的一部分。我什至删除了标签以确保按钮没有重叠(也没有用)

我还尝试在“代码调试层次结构”(3D 模式)中查看代码,即使我已经添加了它,我也看不到按钮

下面是我的代码,其中包含标签、 ScrollView 和按钮的示例。如果有人能提供任何见解,那就太好了......无论哪种方式谢谢....

.............. ScrollView ................................

var editInfoView : UIScrollView = {

let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
view.contentSize.height = 700
view.backgroundColor = tableBackGroundColor
view.frame = CGRect(x: 0, y: 220, width: 375, height: 400)

return view
}()

................................标签................................

vehicleNumberLabel.translatesAutoresizingMaskIntoConstraints = false
vehicleNumberLabel.textColor = .white
vehicleNumberLabel.text = "Vehicle Number"
vehicleNumberLabel.textAlignment = .left

editInfoView.addSubview(vehicleNumberLabel)

vehicleNumberLabel.leftAnchor.constraint(equalTo: editInfoView.leftAnchor).isActive = true
vehicleNumberLabel.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 100).isActive = true
vehicleNumberLabel.widthAnchor.constraint(equalToConstant: 160).isActive = true
vehicleNumberLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

.....................按钮...................... …………

vehicleNumberButton.translatesAutoresizingMaskIntoConstraints = false
vehicleNumberButton.setTitleColor(tableTextColor, for: .normal)
vehicleNumberButton.setTitle("Vehicle Number", for: .normal)
vehicleNumberButton.tintColor = tableTextColor
vehicleNumberButton.backgroundColor = tableTextColor

editInfoView.addSubview(vehicleNumberButton)

vehicleNumberButton.rightAnchor.constraint(equalTo: editInfoView.rightAnchor).isActive = true
vehicleNumberButton.topAnchor.constraint(equalTo: editInfoView.topAnchor, constant: 400).isActive = true
vehicleNumberButton.widthAnchor.constraint(equalToConstant: 600).isActive = true
vehicleNumberButton.heightAnchor.constraint(equalToConstant: 255).isActive = true

最佳答案

虽然我无法通过提供的代码和解释确定您的问题的根本原因,但我怀疑您的 UIScrollView() 的框架在 viewDidAppear(_:) 之后为零将 subview 添加到 CGRect.zero 可能会导致布局引擎出现一些奇怪的行为。当我们以编程方式创建约束时,我们正在创建不等式、等式和优先级的组合,以将 View 限制在特定框架内。如果这些约束方程的值不正确,它会改变相关 View 的显示方式。最好避免同时使用 leftAnchorrightAnchor,因为 View 可能会根据语言(书写方向)和用户设置翻转方向。

ViewController.swift

import UIKit

class ViewController: UIViewController {

var editInfoScrollView : UIScrollView = {
let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
view.isUserInteractionEnabled = true
view.alwaysBounceVertical = true
view.isScrollEnabled = true
view.contentSize.height = 700
view.backgroundColor = UIColor.red.withAlphaComponent(0.3)
// Does nothing because `translatesAutoresizingMaskIntoConstraints = false`
// Instead, set the content size after activating constraints in viewDidAppear
//view.frame = CGRect(x: 0, y: 220, width: 375, height: 400)
return view
}()

var vehicleNumberLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.black
label.text = "Vehicle Number"
label.textAlignment = .left
return label
}()

lazy var vehicleNumberButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.tag = 1
button.setTitleColor(UIColor.black, for: .normal)
button.setTitle("Go to Vehicle", for: .normal)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.clear
button.layer.cornerRadius = 30 // about half of button.frame.height
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 2.0
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(handelButtons(_:)), for: .touchUpInside)
return button
}()

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white

self.setupSubviews()

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.editInfoScrollView.contentSize = CGSize(width: self.view.frame.width, height: 700.0)
}

func setupSubviews() {
self.view.addSubview(editInfoScrollView)
editInfoScrollView.addSubview(vehicleNumberLabel)
editInfoScrollView.addSubview(vehicleNumberButton)

let spacing: CGFloat = 12.0

let constraints:[NSLayoutConstraint] = [

editInfoScrollView.widthAnchor.constraint(equalTo: self.view.widthAnchor),
editInfoScrollView.heightAnchor.constraint(equalToConstant: 400.0),
editInfoScrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
editInfoScrollView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 220.0),

vehicleNumberLabel.leadingAnchor.constraint(equalTo: editInfoScrollView.leadingAnchor, constant: spacing),
vehicleNumberLabel.trailingAnchor.constraint(equalTo: editInfoScrollView.trailingAnchor, constant: -spacing),
vehicleNumberLabel.centerXAnchor.constraint(equalTo: editInfoScrollView.centerXAnchor, constant: -50),
vehicleNumberLabel.heightAnchor.constraint(equalToConstant: 75.0),

vehicleNumberButton.widthAnchor.constraint(equalTo: editInfoScrollView.widthAnchor, multiplier: 0.66),
vehicleNumberButton.heightAnchor.constraint(equalToConstant: 65.0),
vehicleNumberButton.topAnchor.constraint(equalTo: vehicleNumberLabel.bottomAnchor, constant: spacing),
vehicleNumberButton.centerXAnchor.constraint(equalTo: editInfoScrollView.centerXAnchor),

]

NSLayoutConstraint.activate(constraints)
}



@objc func handelButtons(_ sender: UIButton) {

switch sender.tag {
case 0:
print("Default button tag")
case 1:
print("vehicleNumberButton was tapped")
default:
print("Nothing here yet")
}

}


}

关于swift - 在 UIScrollView 中看不到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53755635/

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