在 UIScrollView 中看不到按钮
Posted
技术标签:
【中文标题】在 UIScrollView 中看不到按钮【英文标题】:Cannot see Buttons in UIScrollView 【发布时间】:2018-12-13 05:45:00 【问题描述】:我在 swift 中以滚动视图的形式制作了一个列表,其中视图包含各种类型,例如标签、按钮等。
但是,当我将按钮添加到子视图时,尽管显示了所有其他标签等,但它们并未显示。我还尝试在约束和锚点中乱来。 另一方面,当我将相同的按钮添加到 self.view.addsubview 而不是 scrollview.addsubview 时,它们只是显示不滚动,因为不再是滚动视图的一部分。 我什至删除了标签以确保按钮没有重叠(也不起作用)
我还尝试在“代码调试层次结构”(3D 模式)中查看代码,即使添加了按钮,我也看不到那里的按钮
下面是我的代码,带有标签、滚动视图和按钮的示例。如果有人能提供任何见解,那就太好了……谢谢……
................滚动视图..........................
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
【问题讨论】:
为什么要在添加到视图之后再添加约束?也许您应该在设置约束后首先将其添加到视图中 Jonathan,我在将约束添加到视图后添加了约束,因为如果我之前添加到视图,它会给我这个错误(线程 1:信号 SIGABRT)。这种方式适用于标签但不适用于按钮? 啊,好吧,您在哪个功能中添加按钮和标签?在 viewDidLoad 中? 我在 VC 中声明它们,在 viewdidload 中定义并以单独的方法添加到视图中,仅用于约束和设置,标签和按钮也是如此 看看这个,我认为你应该在init
函数中添加你的元素:medium.com/written-code/…
【参考方案1】:
虽然我无法确定您提供的代码和解释问题的根本原因,但我怀疑您的 UIScrollView()
的框架在 viewDidAppear(_:)
添加子视图到 CGRect.zero
后为零可能会导致布局引擎出现一些奇怪的行为.当我们以编程方式创建约束时,我们正在创建不等式、等式和优先级的组合,以将视图限制到特定框架。如果这些约束方程的值不正确,它会改变相关视图的显示方式。避免使用leftAnchor
和rightAnchor
也是一种很好的做法,因为视图可能会根据语言(书写方向)和用户设置来翻转方向。
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")
【讨论】:
@vgvishesh231 上述代码是否有助于解决问题? 兄弟你的代码在 iphone 上工作,所以我想我会在此基础上重写我的整个视图,我很快就会告诉你,真的很感激它 这可行,但部分是,我现在可以添加按钮,但它们的大小是一个大矩形并与其他不可更改的重叠,我将尝试使用标签并使其可触摸等以上是关于在 UIScrollView 中看不到按钮的主要内容,如果未能解决你的问题,请参考以下文章
在上一个会话中单击分离后,在 JetBrains/DotTrace Profiler Controller 中看不到“获取快照”按钮?