swift – 在UIScrollView中看不到按钮

swift – 在UIScrollView中看不到按钮,第1张

概述我在 swift中以scrollview的形式制作了一个列表,其中视图由各种类型组成,例如标签,按钮等. 但是,当我将按钮添加到子视图时,虽然显示了所有其他标签等,但它们不会显示.我也试过在约束和锚点搞乱. 另一方面,当我将相同的按钮添加到self.view.addsubview而不是scrollview.addsubview时,它们显示为不滚动,因为不再是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 UIKitclass 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,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,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中看不到按钮全部内容,希望文章能够帮你解决swift – 在UIScrollView中看不到按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存