我希望有人可以帮助我:
我正在Xcode中构建一个应用程序,我通过代码添加了一些UIViews,我无法通过main.storyboard来实现,因为UIViews的数量,它取决于数据库有多少个字段。因此,如果数据库有20个字段,那么将有20个UIViews。这就是我所做的并且它运行良好,但是......这就是问题: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,除非它可以获得自定义样式(这是它的样子):
绝对使用UITableView
。当然,可以创建自定义设计 - 只需实现您可以根据需要设计的自定义UITableViewCell
。
即使您的解决方案正常工作,如果您有许多数据库条目,性能也会很糟糕。 UITableView
做了很多优化,比如重用不可见的细胞。