通过代码添加 UIViews 时的重叠问题
Overlapping issue when adding UIViews through code
我希望有人能帮助我:
我正在 Xcode 中构建一个应用程序,我通过代码添加了一些 UIView,我无法通过 main.storyboard 完成,因为 UIView 的数量取决于如何数据库有很多字段。因此,例如,如果数据库有 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 来代替,除非有可能获得自定义样式(这是它必须看起来的样子) :
绝对使用UITableView
。当然,可以创建自定义设计 - 只需实现自定义 UITableViewCell
,您可以根据需要进行设计。
即使您的解决方案可以工作,如果您有很多数据库条目,性能也会很糟糕。 UITableView
做了很多优化,比如重用不可见的单元格。
我希望有人能帮助我:
我正在 Xcode 中构建一个应用程序,我通过代码添加了一些 UIView,我无法通过 main.storyboard 完成,因为 UIView 的数量取决于如何数据库有很多字段。因此,例如,如果数据库有 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 来代替,除非有可能获得自定义样式(这是它必须看起来的样子) :
绝对使用UITableView
。当然,可以创建自定义设计 - 只需实现自定义 UITableViewCell
,您可以根据需要进行设计。
即使您的解决方案可以工作,如果您有很多数据库条目,性能也会很糟糕。 UITableView
做了很多优化,比如重用不可见的单元格。