Custom view's IBOutlet is nil - swift

So I have a main viewController that has a stack view to which I want to add multiple instances of custom views I have created.
So my thought process was to create a custom view that inherits from UIView. I want the view to always be 40x40 so I created a new init to take care of this? (not sure if this is correct):
class QuickAddView: UIView {
#IBOutlet weak var iconLabel: UILabel!
var task: Task = Task()
public init(task: Task) {
self.task = task
super.init(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
configureView()
}
private func configureView() {
self.layer.cornerRadius = self.frame.size.width / 2
self.backgroundColor = task.color
configureViewElements()
}
private func configureViewElements() {
configureIconLabel()
}
private func configureIconLabel() {
// CRASH: - iconLabel is nil here
self.iconLabel.text = task.icon
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I then have a QuickAddView nib that sets its custom class to QuickAddView.swift
Lastly, I create the custom views in my viewController:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configureViewElements()
}
private func configureViewElements() {
configureQuickAddStackView()
}
private func configureQuickAddStackView() {
let quickAddView = QuickAddView(task: Task(name: "Go to the store", icon: "🍐", color: .purple))
quickAddStackView.addArrangedSubview(quickAddView)
}
The problem I'm having is that my iconLabel is nil when I try to set up my QuickAddView. I also don't know if I'm doing this process of creating a custom view correct.

Since you are connecting the IBOutlet from xibFile you have to use
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
configureView()
}
Also in your class you have to instantiate it with
if let quickAddView = Bundle.mainBundle().loadNibNamed("QuickAddView", owner: self, options: nil).first as? QuickAddView {
quickAddView.task = task
}

The problem is that you're creating a custom view in code, but using an IBOutlet for the iconLabel. An IBOutlet is for something you've built in Interface Builder, not in code. If you want to create the QuickAddView in code, you need to also create the iconLabel in code, in the QuickAddView class. You need to make these modifications:
weak var iconLabel: UILabel?
private func configureIconLabel() {
iconLabel = UILabel(frame: CGRect())
if let iconLabel = iconLabel {
iconLabel.text = task.icon
addSubview(iconLabel)
}
}
Note that I passed in a zeroed out CGRect, but you'll want to use whatever you want the UILabels origin and size to be, or use autolayout to configure where the iconLabel is displayed.

Related

How to create this view with constraints programtically

I feel this is pretty simple to accomplish but I can't seem to figure it out. I'm fairly new to not using the storyboard and trying to learn how to set my constraints programatically for my views. I created the view that I want easily in storyboard but can't seem to get it programatically.
I have my view controller has my parent view, and then I call a container view. I imagine in the container view is where I setup my constraints but I can't get the height of my view to stay the same every-time I change to a different device
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var clariView = ClariContainerView()
view.addSubview(clariView)
}
}
This my view controller and then my ClariContainerView looks like this:
class ClariContainerView: UIView {
lazy var clariQuestionView: UIView = {
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0))
containerView.backgroundColor = .blue
containerView.translatesAutoresizingMaskIntoConstraints = false
return containerView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
public func setupView() {
addSubview(clariQuestionView)
setupLayout()
}
public func setupLayout() {
NSLayoutConstraint.activate([
clariQuestionView.heightAnchor.constraint(equalToConstant: 169)
])
}
}
What I'm trying to recreate is this:
I need the height of the blue view to always be 169.
Here is how you would do that:
First, you don't need to define a frame for your containerView since the translatesAutoresizingMaskIntoConstraints = falsestatement is specifying that you'll be using auto-layout and therefore the frame will be ignored:
lazy var clariQuestionView: UIView = {
let containerView = UIView()
containerView.backgroundColor = .blue
containerView.translatesAutoresizingMaskIntoConstraints = false
return containerView
}()
And here is how you would define your constraints. You need to set height, but also need to pin the view to the bottom, the leading, and the trailing edges of self.view:
public func setupLayout() {
NSLayoutConstraint.activate([
clariQuestionView.heightAnchor.constraint(equalToConstant: 169),
clariQuestionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
clariQuestionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
clariQuestionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
}
For such a basic layout you don't really need to add heightAnchor. Here is a simple way to achieve desired behavior + bonus β€” a code snippet to adjust height according to the device's safeAreaInsets.
class ClariContainerView: UIView {
lazy var clariQuestionView: UIView = {
let desiredContainerHeigh = 169
// If you want, you can use commented code to adjust height according to the device's safe area.
// This might be needed if you want to keep the same height over safe area on all devices.
// let safeAreaAdjustment = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom ?? 0
let containerView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 169, width: UIScreen.main.bounds.width, height: 169))
containerView.backgroundColor = .blue
containerView.translatesAutoresizingMaskIntoConstraints = true
return containerView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
public func setupView() {
addSubview(clariQuestionView)
}
}

How to bring a subview to front when selected in Swift?

So I've got two views, both added to the superview programmatically:
//First added view to my superView (baseView)
let imageViewOne = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
baseView.addSubview(imageViewOne)
//Second added view to my superView (baseView)
let imageViewTwo = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
baseView.addSubview(imageViewTwo)
I want to bring one of them to the front when it is selected because when you add a new view, it overlaps the previous one. Something that works with "selected" or "touched".
Use this:
baseView.bringSubview(toFront: imageViewOne)
You can do this by UITapGestureRecognizer by setting Restoration ID from the interface builder
func tapOneAct(sender: UITapGestureRecognizer) {
if(sender.view!.restorationIdentifier == "view1")
// view1 is `Restoration ID` of imageViewOne
{
view.bringSubview(toFront: imageViewOne)
}
if(sender.view!.restorationIdentifier == "view2")
// view2 is `Restoration ID` of 2nd view
{
view.bringSubview(toFront: imageViewTwo)
}
}
If I understand correctly then you would like to check the status of selected view and bring it to the front of the view hierarchy.
In this case you have to do following things:
1) track selection state, let it will be var isSelected: Bool in code snippet
2) hold a callback for an action when view is selected (you can use for example UIButton to observe tap event)
In following code snippet you can check the selection state when the view is touched.
BaseView implements logic for checking the state of SelectionView.
import UIKit
final class SelectionView<T: UIView>: UIView {
var onTap: ((_ isSelected: Bool) -> ())?
// MARK: - Private
private let view: T
private let button = UIButton(type: .custom)
private var isSelected: Bool = false
// MARK: - Init
init(view: T) {
self.view = view
super.init(frame: .zero)
setUp()
}
// MARK: - Private
private func setUp() {
addSubview(view)
addSubview(button)
button.addTarget(self, action: #selector(onButtonTap), for: .touchUpInside)
}
#objc private func onButtonTap() {
isSelected = !isSelected
onTap?(isSelected)
}
// MARK: - Layout
override func layoutSubviews() {
super.layoutSubviews()
view.frame = bounds
button.frame = bounds
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return view.sizeThatFits(size)
}
// MARK: - Unused
override init(frame: CGRect) {
fatalError("init(frame:) has not been implemented")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class BaseView: UIView {
init() {
super.init(frame: .zero)
let views = [SelectionView(view: UIView()), SelectionView(view: UIView())]
for view in views {
view.onTap = { [weak self, weak view] isSelected in
guard let selectedView = view
else { return }
if isSelected {
self?.bringSubview(toFront: selectedView)
}
}
}
}
// MARK: - Unused
override init(frame: CGRect) {
fatalError("init(frame:) has not been implemented")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

view.frame is not set when IBDesignable loads xib

I have a simple UIView subclass that I have made #IBDesignable.
I overloaded init frame and init coder. My code loads the view description from a .xib file.
After the .xib file has been loaded I try to set:
view.clipsToBounds = true
view.layer.cornerRadius = frame.size.height / 2
to get a "rectangle with circular sides".
The problem is that frame.size.height = 0 at that time (after .xib is loaded and my custom view drawn for the first time by Interface Builder).
Is there a workaround?
Here it is my code:
#IBDesignable
class CustomUIVIew: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
private var view: UIView!
private let nibName = "CustomUIView"
private func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
setupView()
}
private func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
private func setupView() {
view.clipsToBounds = true
view.layer.cornerRadius = frame.size.height / 2
}
}
Thank you, Luca-
You should move any size related adjustments into layoutSubviews. This way, not only will it be rendered correctly in IB, but also on device, if the size of the window changes, the cornerRadius will be adjusted accordingly.
prepareForInterfaceBuilder did the job
override func prepareForInterfaceBuilder() {
view.clipsToBounds = true
view.layer.cornerRadius = bounds.height / 2
}

customize UIPageControl dots

I'm currently trying to customize the UIPageControl so it will fit my needs. However I seem to be having a problem when implementing some logic in the draw
What I want to do is to be able to user IBInspectable variables to draw out the UIPageControl however those seem to still be nil when the draw method is being called and when I try to implement my logic in the awakeFromNib for instance it won't work.
What I did so far is the following
class BoardingPager: UIPageControl {
#IBInspectable var size: CGSize!
#IBInspectable var borderColor: UIColor!
#IBInspectable var borderWidth: CGFloat! = 1
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.pageIndicatorTintColor = UIColor.clear
}
override func draw(_ rect: CGRect) {
super.draw(rect)
setDots()
}
func setDots() {
for i in (0..<self.numberOfPages) {
let dot = self.subviews[i]
if size != nil {
let dotFrame = CGRect(x: size.width/2, y: size.height/2, width: size.width, height: size.height)
dot.frame = dotFrame
}
if i != self.currentPage {
dot.layer.cornerRadius = dot.frame.size.height / 2
dot.layer.borderColor = borderColor.cgColor
dot.layer.borderWidth = borderWidth
}
}
}
}
Another problem I'm facing is that I want to remove/add a border when the current page changes.
I'm hoping someone will be able to help me out

UIView from NIB not conforming to parent constraints after adding autolayout constraints

I am loading my NIB file to my view using :
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("EventPopup", owner: self, options: nil)
//self.frame = self.view.bounds - DIDN'T WORK
//self.frame.size.width = 300 - DID'T WORK
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
Bundle.main.loadNibNamed("EventPopup", owner: self, options: nil)
self.addSubview(self.view); // adding the top level view to the view hierarchy
}
This works and adds my NIB but the width of the UIView stretches outside of the parent container. This only occurs after adding autolayout to the uiview. I hav checked and the initial frame is size 600x600.
Even after trying to reset the NIBs size by calling layoutSubviews() and putting the new frame constraints in it still refuses to resize. For example:
override func layoutSubviews() {
// self.frame.size.width = 300
//self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: 200, height: self.frame.height)
}
Take a look at this post here. Look at the 3rd tip which shows how to create a view that loads from a nib. The code is Reproduced below (disclaimer it is written in Swift 2):
#IBDesignable
class ProfileView: UIView {
#IBOutlet var imgView: UIImageView!
#IBOutlet var labelOne: UILabel!
#IBOutlet var labelTwo: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: β€œProfile”, bundle: bundle)
let viewFromNib = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
addSubview(viewFromNib)
viewFromNib.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
β€œH:|[v]|”,
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: [β€œv”:viewFromNib]
)
)
self.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
β€œV:|[v]|”,
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil, views: [β€œv”:viewFromNib]
)
)
}
}
Once you have it set up properly in code and the nib, you should be able to initialize it easily. Either in Interface builder or in code:
// ViewController
override viewDidLoad() {
super.viewDidLoad()
self.addSubview(ProfileView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)))
}