【问题标题】:Overlapping issue when adding UIViews through code通过代码添加 UIView 时出现重叠问题
【发布时间】:2018-09-13 09:08:48
【问题描述】:

我希望有人可以帮助我:

我正在 Xcode 中构建一个应用程序,我通过代码添加了一些 UIViews,我无法通过 main.storyboard 来完成,因为 UIViews 的数量取决于数据库有多少字段。因此,例如,如果数据库有 20 个字段,则将有 20 个 UIView。这就是我所做的并且效果很好,但是......这是问题所在:UIViews 与已添加到 Main.storyboard 中的其他对象重叠,并且当我尝试时某些对象(如分段控件)不起作用与他们互动。

这是我目前得到的代码:

import UIKit  


class issuesScreen: UIViewController {  
    @IBOutlet weak var csViewLeading: NSLayoutConstraint!  
    var showingMenu=false  
    lazy var scrollView: UIScrollView = {  
        let view = UIScrollView()  
        view.translatesAutoresizingMaskIntoConstraints = false  
        view.contentSize.height = 2000  
        return view  
    }()  

    override func viewDidLoad() {  
        super.viewDidLoad()  
        view.addSubview(scrollView)  
        setupScrollView()  
    }  
    //Here is when I'm adding the views  
    func setupScrollView() {  
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true  
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true  
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true  
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true  
        let firstView = UIView()  
        firstView.translatesAutoresizingMaskIntoConstraints = false  
        firstView.backgroundColor = .black  

        scrollView.addSubview(firstView)  

        firstView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true  
        firstView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 40).isActive = true  
        firstView.widthAnchor.constraint(equalToConstant: 400).isActive = true  
        firstView.heightAnchor.constraint(equalToConstant: 180).isActive = true  

        let secondView = UIView()  
        secondView.translatesAutoresizingMaskIntoConstraints = false  
        secondView.backgroundColor = .white  
        scrollView.addSubview(secondView)  

        secondView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true  
        secondView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 41).isActive = true  
        secondView.widthAnchor.constraint(equalToConstant: 398).isActive = true  
        secondView.heightAnchor.constraint(equalToConstant: 178).isActive = true  
    }  
    @IBAction func showBar(_ sender: UIBarButtonItem) {  
        if(showingMenu) {  
            csViewLeading.constant = -240  
        } else {  
            csViewLeading.constant = 0  
            UIView.animate(withDuration: 0.3, animations: {self.view.layoutIfNeeded()})  
        }  
        showingMenu= !showingMenu  
    }  
}

这是我测试应用时的结果:

,

灰色背景是侧边栏,如您所见,它与 UIView 重叠。

我真的不知道为什么会发生这种情况,而且,不要告诉我使用 tableView 代替,除非可以获得该自定义样式(这就是它的外观):

【问题讨论】:

    标签: ios swift uiview overlapping


    【解决方案1】:

    一定要使用UITableView。当然可以创建自定义设计 - 只需实现自定义 UITableViewCell,您可以根据需要进行设计。

    即使您的解决方案能够正常工作,如果您有很多数据库条目,性能也会很糟糕。 UITableView 做了很多优化,比如重用不可见的单元格。

    【讨论】:

    • 好的,非常感谢@pajevic!因此,我将使用 UITableView,但我必须学习如何使用它,因为我对 XCode 还很陌生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多