Reputation: 483
I have a simple class that designs the display of title and content and other controller class that displays the content after fetching it from coredata, every thing works fine but the fields are not editable, where am i making the error,
Design Class
import UIKit
class UpdateNoteDesignView: UIView {
let notesUpdateTitle = UITextField()
let notesUpdateContent = UITextView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func commonInit() {
let updateStack = UIStackView()
updateStack.axis = .vertical
updateStack.alignment = .top
updateStack.distribution = .fill
updateStack.spacing = 5
notesUpdateTitle.translatesAutoresizingMaskIntoConstraints = false
notesUpdateContent.translatesAutoresizingMaskIntoConstraints = false
notesUpdateTitle.widthAnchor.constraint(greaterThanOrEqualToConstant: 150).isActive = true
notesUpdateContent.widthAnchor.constraint(greaterThanOrEqualToConstant: 300).isActive = true
notesUpdateTitle.heightAnchor.constraint(equalToConstant: 30).isActive = true
notesUpdateContent.heightAnchor.constraint(greaterThanOrEqualToConstant: 300).isActive = true
notesUpdateTitle.font = UIFont(name: "Arial", size: 30)
notesUpdateContent.font = UIFont(name: "Arial", size: 30)
notesUpdateContent.layer.cornerRadius = 5
notesUpdateContent.layer.borderWidth = 2
notesUpdateTitle.layer.borderWidth = 2
notesUpdateTitle.layer.cornerRadius = 5
updateStack.translatesAutoresizingMaskIntoConstraints = false
updateStack.addArrangedSubview(notesUpdateTitle)
updateStack.addArrangedSubview(notesUpdateContent)
addSubview(updateStack)
updateStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
updateStack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
}
}
Controller Class
import UIKit
class UpdateNotesController: UIViewController {
let updateDesign = UpdateNoteDesignView()
var note: Note?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(updateDesign)
updateDesign.notesUpdateTitle.text = note?.title
updateDesign.notesUpdateContent.text = note?.contents
}
}
Upvotes: 0
Views: 384
Reputation: 580
Here, the issue is that frame is not set for your view. Try to add background color to UpdateNoteDesignView. You will not able to see it.
Please add frame/constraint for UpdateNoteDesignView as shown here and make changes according to your requirement.
class ViewController: UIViewController {
var updateDesign: UpdateNoteDesignView!
// var note: Note?
override func viewDidLoad() {
super.viewDidLoad()
updateDesign = UpdateNoteDesignView()//frame: CGRect(x: 0, y: 0, width: 200, height: 260))
view.backgroundColor = UIColor.white
view.addSubview(updateDesign)
updateDesign.translatesAutoresizingMaskIntoConstraints = false
updateDesign.heightAnchor.constraint(equalToConstant: 450).isActive = true
updateDesign.widthAnchor.constraint(equalToConstant: 350).isActive = true
updateDesign.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
updateDesign.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
updateDesign.notesUpdateTitle.text = "Stack overflow"
updateDesign.notesUpdateContent.text = "Developer"
}
}
Upvotes: 2