I am new to swift. I have added view to stackview using addArrangedSubview()
. But I am not able to remove this view using removeArrangedSubview()
.
Even after removing arranged subView the view is still present
import Foundation
import UIKit
class render: UIViewController {
let subview = UIStackView()
let mainview = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
self.mainviewlet()
self.login()
}
func login() {
let username = UITextField()
// text field
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
//button
// Adding to subview
subview.axis = UILayoutConstraintAxis.vertical
subview.distribution = UIStackViewDistribution.equalSpacing
subview.alignment = UIStackViewAlignment.center
subview.spacing = 16.0
subview.addArrangedSubview(username)
subview.addArrangedSubview(button)
subview.translatesAutoresizingMaskIntoConstraints = false;
// Adding subview to another stackview
mainview.addArrangedSubview(subview)
self.view.addSubview(mainview)
}
In another function I am removing the arranged subview
func signup(sender: UIButton!) {
// Remove subview
mainview.removeArrangedSubview(subview)
subview.removeFromSuperview()
let firstname = UITextField()
firstname.textAlignment = NSTextAlignment.center
firstname.textColor = UIColor.black
firstname.frame = CGRect()
firstname.frame.size.height = 30;
firstname.text = "firstname"
subview.addArrangedSubview(firstname)
mainview.addArrangedSubview(subview)
self.view.addSubview(mainview)
}
and my mainview is created as:
func mainviewlet {
mainview.axis = UILayoutConstraintAxis.vertical
mainview.distribution = UIStackViewDistribution.equalSpacing
mainview.alignment = UIStackViewAlignment.center
mainview.spacing = 16.0
mainview.translatesAutoresizingMaskIntoConstraints = false;
self.view.addSubview(mainview)
}
I want the username
& button
to be deleted and add new field firstname
to the subview.
Am I doing it the right way ? How to delete subview ? Thanks for any help
subview.removeArrangedSubview(username)
. – Tatouay