Custom UIView Class Constraints Depending on ParentView - ios

I'm making a custom UIView class that hold a datepicker. I want to setup the view constraints to reference the parent view that will call my custom class. here is my following code. this is what i get
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
class CustomView: UIView {
var datepicker:UIDatePicker!
override init(frame: CGRect) {
super.init(frame: frame)
setupConstraints()
} // initiliaze the button like this CustomButton()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupDatePicker() {
let datepicker = UIDatePicker()
datepicker.minimumDate = Date()
}
func setupConstraints(){
setupDatePicker()
translatesAutoresizingMaskIntoConstraints = false
leadingAnchor.constraint(equalTo: superview!.leadingAnchor, constant: 20).isActive = true // the error occurs here
trailingAnchor.constraint(equalTo: superview!.trailingAnchor, constant: -20).isActive = true
heightAnchor.constraint(equalToConstant: 60).isActive = true
centerYAnchor.constraint(equalTo: superview!.centerYAnchor).isActive = true
backgroundColor = .red
addSubview(datepicker)
datepicker.translatesAutoresizingMaskIntoConstraints = false
datepicker.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10).isActive = true
datepicker.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10).isActive = true
datepicker.heightAnchor.constraint(equalToConstant: 50).isActive = true
datepicker.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
}
}
and this is where i create my custom view and launch it
var customView: CustomView!
class ListViewController: UIViewController {
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
customView = CustomView()
view.addSubview(customView)
view.bringSubviewToFront(customView)
Thank you for your time.

You're not assigning var datepicker:UIDatePicker! so it is nil.
Try:
func setupDatePicker() {
let datepicker = UIDatePicker()
datepicker.minimumDate = Date()
self.datepicker = datepicker
}

Related

addSubview doesn't work in UIView - Swift Programmatically

I need to add a UIButton as a subview on a UIView but it actually doesn't appear at runtime.
This is my code:
let moreButton : UIButton = {
let button = UIButton()
button.setImage(#imageLiteral(resourceName: "more"), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(moreButton)
moreButton.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
moreButton.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
}
The button isn't added eventually to the view. I'm sure this is an easy fix but I can't wrap my head around it.
First of all, make sure you the viewController is showing anything since you're doing it without storyboards, check out this simple tutorial:
https://medium.com/better-programming/creating-a-project-without-storyboard-in-2020-and-without-swifui-82080eb6d13b
If the problem is with that UIButton, try to set up the subviews in viewDidLoad:
final class ViewController: UIViewController {
let cardView = CardView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(cardView)
/// Constraints
let margins = view.layoutMarginsGuide
cardView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
cardView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
cardView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
cardView.heightAnchor.constraint(equalToConstant: 30).isActive = true
cardView.widthAnchor.constraint(equalToConstant: 20).isActive = true
}
}
final class CardView: UIView {
let moreButton : UIButton = {
let button = UIButton()
button.setTitle("Button title", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(frame: CGRect) {
super.init(frame:frame)
self.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(moreButton)
/// Constraints
let margins = self.layoutMarginsGuide
moreButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
moreButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
moreButton.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
}
}
I've set up margins for the constraints and also added a leadingAnchor constraint, that might have been the issue as well.

self.perform segue causes view to not conform

I have programmatically created a view layout with a content view, when this controller is the initial view controller I get something like this.
:
When I have another view controller and segue to this viewcontroller, I get something like this:
Attached is my View Class:
class TestView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
setupConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
self.addSubview(contentView)
}
func setupConstraints() {
self.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
// check what safearealayoutguide is
if #available(iOS 11.0, *) {
print("IOS11 Screen")
contentView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
} else {
contentView.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor, constant: 0).isActive = true
}
contentView.rightAnchor.constraint(equalTo: self.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
if #available(iOS 11.0, *) {
contentView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
} else {
contentView.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor, constant: 0).isActive = true
}
}
let contentView: UIView = {
let view = UIView(frame: CGRect(x: 100, y: 100, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width))
//view.layer.backgroundColor = CGColortran
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.green.cgColor
return view
}()
}
then my view controller:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func loadView() {
self.view = TestView(frame: UIScreen.main.bounds)
}
}
When this is the only screen in the application, it works. when I add another view controller and do a self.performSegue(withIdentifier: "LoginUser", sender: nil) it seems to break the view. What's going on here?
Comment
self.translatesAutoresizingMaskIntoConstraints = false
as currently you mix setting a frame TestView(frame: UIScreen.main.bounds) with constraints , the above line is used only for a view when you set constraints to it as it invalidates the frame

class added dynamically doesn't respond touched in swift

I declare TestClass contain button and textField and constraint it and then add this class to view controller
import Foundation
import UIKit
class TestClass : UIView {
let testButton : UIButton = {
let button = UIButton()
button.setTitle("Test", for: .normal)
button.setTitleColor(.red ,for:.normal)
return button
}()
let testInput : UITextField = {
let input = UITextField()
input.placeholder = "type here"
return input
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
func setupView(){
addSubview(testButton)
addSubview(testInput)
testButton.translatesAutoresizingMaskIntoConstraints = false
testInput.translatesAutoresizingMaskIntoConstraints = false
testButton.topAnchor.constraint(equalTo: self.topAnchor, constant: 20).isActive = true
testButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
testButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
testButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
testInput.topAnchor.constraint(equalTo: self.topAnchor, constant: 70).isActive = true
testInput.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
testInput.heightAnchor.constraint(equalToConstant: 30).isActive = true
testInput.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
in the viewDidLoad I add this class and it appeared .
but when I tap on textField or button it doesn't respond
class ViewControll : UIViewController {
let newClass : TestClass = {
let view = TestClass()
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(newClass)
newClass.frame.size = CGSize(width: 200, height: 200)
newClass.translatesAutoresizingMaskIntoConstraints = false
newClass.topAnchor.constraint(equalTo: contView.topAnchor, constant: 300).isActive = true
newClass.leadingAnchor.constraint(equalTo: contView.leadingAnchor, constant: 30).isActive = true
newClass.testButton.addTarget(self, action: #selector(prnt), for: .touchUpInside)
}
#objc func prnt(){
print("bla bla bla")
}
}
when I click on button or tap on textField nothing happened
can anyone help me please
Constrains don't define height and width of your TestClass - set frame doesn't work. You need to add constraints for this as well, for example, in viewDidLoad method:
newClass.heightAnchor.constraint(equalToConstant: 200).isActive = true
newClass.widthAnchor.constraint(equalToConstant: 200).isActive = true

UILabel TapGesture not firing

Any idea why the Tap gesture on a UI Label will not fire?
I've tried using a delegate also but in its simplest form, for some reason it will just not hit the action method.
Is the UIView layer restricting this interaction?
class TestTextViewLabel : UIView {
weak var testTextView: UITextView!
weak var testLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))
let testUITextView: UITextView = {
let textView = UITextView()
textView.textColor = UIColor(hex: "#000")
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
let testUILabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(hex: "#666666")!
label.translatesAutoresizingMaskIntoConstraints = false
label.addGestureRecognizer(tapGesture)
label.isUserInteractionEnabled = true
return label
}()
self.addSubview(testUITextView)
self.addSubview(testUILabel)
self.testTextView = testUITextView
self.testLabel = testUILabel
testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
print("testLabelTapped")
}
}
I tried running your class and I was able to get the UILabel to fire after putting text into it. The tap gesture is only recognized within the view's bounds and since you didn't have any text in the UILabel, the bounds were zero giving you no place to click it. By default, if you put in text, the UILabel will automatically match those bounds. Here is my working code below:
class TestTextViewLabel : UIView {
weak var testTextView: UITextView!
weak var testLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))
let testUITextView: UITextView = {
let textView = UITextView()
textView.textColor = UIColor.black
textView.translatesAutoresizingMaskIntoConstraints = false
textView.text = "This is a text view"
textView.backgroundColor = .clear
return textView
}()
let testUILabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(red:0.40, green:0.40, blue:0.40, alpha:1.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.addGestureRecognizer(tapGesture)
label.isUserInteractionEnabled = true
label.text = "This is a UILabel view"
return label
}()
self.addSubview(testUITextView)
self.addSubview(testUILabel)
self.testTextView = testUITextView
self.testLabel = testUILabel
testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
print("testLabelTapped")
}
}

UICollectionViewCell no common ancestor error (NOT duplicate)

class ChatCollectionViewCell: UICollectionViewCell {
var chatView: UIView!
var chatTextView: UITextView!
var isTextFromCurrentUser: Bool = true
var chatViewWidth: CGFloat = 200
override init(frame: CGRect) {
super.init(frame: frame)
chatView = UIView()
chatTextView = UITextView()
contentView.addSubview(chatView)
contentView.addSubview(chatTextView)
setupViews()
}
override func layoutSubviews() {
if isTextFromCurrentUser {
chatView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
chatTextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15).isActive = true
chatTextView.backgroundColor = .white
} else {
chatView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
chatTextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
chatTextView.backgroundColor = UIColor(r: 157, g: 255, b: 164)
}
chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
chatView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
chatTextView.widthAnchor.constraint(equalTo: chatView.widthAnchor, constant: -10).isActive = true
chatTextView.topAnchor.constraint(equalTo: chatView.topAnchor, constant: 5).isActive = true
chatView.translatesAutoresizingMaskIntoConstraints = false
chatTextView.translatesAutoresizingMaskIntoConstraints = false
}
func setupViews() {
chatView.backgroundColor = .blue
chatTextView.font = UIFont.systemFont(ofSize: 16)
chatTextView.layer.cornerRadius = 9
chatTextView.clipsToBounds = true
chatTextView.isScrollEnabled = false
}
override func prepareForReuse() {
super.prepareForReuse()
chatView = nil
chatTextView = nil
chatView = UIView()
chatTextView = UITextView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
(for clarification, I am setting the chatViewWidth and the chatTextView.text property in the ViewController's cellForRow method)
So right now, the error that XCode is giving is the following: "Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?"
I looked through many posts regarding common ancestor errors on StackOverflow, but none of the solutions solved my problem.
Which is very confusing.
I tried using breakpoints to analyze what was wrong, but the program crashed after creating around ~14 cells or so. And sometimes it works, but when I add more cells, it will begin crashing. I'm not sure what the issue is--my views are definitely children views of the CollectionViewCell, correct?
Thank you!
This is how i would do it ... well basicy i would set all the stuff in Storyboard and set an Outlet for the widthConstraint ... but to take your code, this should work. But its not testet ... :)
class ChatCollectionViewCell: UICollectionViewCell {
var chatView: UIView!
var chatTextView: UITextView!
var isTextFromCurrentUser: Bool = true {
didSet {
if isTextFromCurrentUser {
NSLayoutConstraint.deactivate(rightAlignmentConstraints)
NSLayoutConstraint.activate(leftAlignmentConstraints)
chatTextView.backgroundColor = .white
} else {
NSLayoutConstraint.deactivate(leftAlignmentConstraints)
NSLayoutConstraint.activate(rightAlignmentConstraints)
chatTextView.backgroundColor = UIColor(r: 157, g: 255, b: 164)
}
}
}
var chatViewWidth: CGFloat = 200 {
didSet {
chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
}
}
private var leftAlignmentConstraints: [NSLayoutConstraint] = []
private var rightAlignmentConstraints: [NSLayoutConstraint] = []
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
chatView = UIView()
chatTextView = UITextView()
contentView.addSubview(chatView)
contentView.addSubview(chatTextView)
chatView.translatesAutoresizingMaskIntoConstraints = false
chatTextView.translatesAutoresizingMaskIntoConstraints = false
chatView.backgroundColor = .blue
chatTextView.font = UIFont.systemFont(ofSize: 16)
chatTextView.layer.cornerRadius = 9
chatTextView.clipsToBounds = true
chatTextView.isScrollEnabled = false
leftAlignmentConstraints = [
chatView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10),
chatTextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15)
]
rightAlignmentConstraints = [
chatView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10),
chatTextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15)
]
chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
chatView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
chatTextView.widthAnchor.constraint(equalTo: chatView.widthAnchor, constant: -10).isActive = true
chatTextView.topAnchor.constraint(equalTo: chatView.topAnchor, constant: 5).isActive = true
NSLayoutConstraint.activate(leftAlignmentConstraints)
}
override func prepareForReuse() {
super.prepareForReuse()
chatTextView.text = ""
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Good Luck and Happy Coding

Resources