Hello,
I'm trying to add UIViews through code, but when I test the app, it overlaps with other objects of the Main.storyboard.
For example, it overlaps with the side bar and with the Segmented Control (I can't use it since I've added those views programmatically)
This is the code I've got so far:
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
}
}
That's how the side bar looks like now: https://i.imgur.com/n24HDvH.png
As you can see, it overlaps with the UIView and as I said, the segmented control doesn't work (I can't change to "TANCADES" or vice versa).
Any help will be appreciated. Thank you in advance!