当前位置 : 主页 > 手机开发 > 其它 >

swift – 在UIScrollView中看不到按钮

来源:互联网 收集:自由互联 发布时间:2021-06-11
我在 swift中以scrollview的形式制作了一个列表,其中视图由各种类型组成,例如标签,按钮等. 但是,当我将按钮添加到子视图时,虽然显示了所有其他标签等,但它们不会显示.我也试过在约束和
我在 swift中以scrollview的形式制作了一个列表,其中视图由各种类型组成,例如标签,按钮等.

但是,当我将按钮添加到子视图时,虽然显示了所有其他标签等,但它们不会显示.我也试过在约束和锚点搞乱.
另一方面,当我将相同的按钮添加到self.view.addsubview而不是scrollview.addsubview时,它们显示为不滚动,因为不再是scrollview的一部分.
我甚至删除了标签,以确保按钮没有重叠(也没有工作)

我也尝试在“代码调试层次结构”(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
虽然我无法确定您提供的代码和解释问题的根本原因,但我怀疑在将视图添加到CGRect.zero的viewDidAppear(_ :)之后,您的UIScrollView()的框架为零可能会导致布局引擎出现一些奇怪的行为.当我们以编程方式创建约束时,我们正在创建不等式,等式和优先级的组合,以将视图限制为特定帧.如果这些约束方程的值不正确,则会更改相关视图的显示方式.它的良好做法是避免使用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")
        }

    }


}
网友评论