iOS: Inheritance with different xib files - ios

UIViewA is defined in .xib and .swift.
I would like to create a UIView with it own .xib that inherits from UIViewA with several extra properties.
So:
class UIViewA: UIView {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var subtitleLabel: UILabel!
}
class UIViewB: UIViewA {
#IBOutlet weak var thirdLabel: UILabel!
}
Both UIViewA and UIViewB have titleLabel and subtitleLabel. They are just positioned differently in there xibs. Is there a way I could drag titleLabel in UIVIewB.xib to the titleLabel IBOutlet in UIViewA.swift?

Yes, you can do it. In your example, if you open UIVIewB.xib and the file owner is the class UIVIewB you can open another editor window with the class UIViewA and you can drag the UI elements to the UIViewA properties.
Like this:

Related

Disable interaction on subview of UITableViewCell

I have a custom table cell with two subView in it. One of them should allow user interaction, and one of the views should not allow interaction.
I have already tried userInteraction enable and disable on UIView but didn't get any result.
#IBOutlet weak var headerView: UIView!
#IBOutlet weak var detailView: UIView!
The orange selected color is my detailView and i want to prevent user interaction on this view.

Clear placeholder text from storyboard or XIB outlets

In my XIB file and storyboards I have some UILabel's with placeholder text that I would like to clear out before the view loads.
Is there some function provided by the SDK for this purpose?
What is best practice for doing this?
You need to connect each UILabel to correspond IBOutlet variable in your code and change the text of it on early stage of view's lifecycle.
#IBOutlet weak var someLabel: UILabel! // connected in xib
override func viewDidLoad() {
someLabel.text = ""
}

Is it possible to set inputView/ inputAccessoryView for textfields in storyboard?

I have picker and toolbar for textfields.
#IBOutlet var picker: UIDatePicker!
#IBOutlet var toolbar: UIToolbar!
#IBOutlet weak var toTF: UITextField!
#IBOutlet weak var fromTF: UITextField!
and then in viewDidLoad
fromTF.inputView = picker
toTF.inputView = picker
fromTF.inputAccessoryView = toolbar
toTF.inputAccessoryView = toolbar
Is it possible to avoid this code and set them in storyboard? (Like you do that for delegates, etc.) I can't find any possibilities.
As far as I can tell, this isn't possible. You can set the views up in your Storyboard/Xib as yo have done, but it looks like the actual connection logic needs to take place in code.

UILabel height depending on text

I am getting data from JSON and it changes for each view so I need the labels to change their height depending on their content and also the rest of labels and buttons should move down. I am using sizeToFit but anything else moves and when I scroll it loses its height and goes to 1 line again.
My labels have 0 lines in storyboard.
#IBOutlet weak var titulares: UILabel!
#IBOutlet weak var scroller: UIScrollView!
#IBOutlet weak var imagen: UIImageView!
#IBOutlet weak var estilo: UILabel!
#IBOutlet weak var historia: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// I get JSON Data
self.historia.sizeToFit()
self.titulares.sizeToFit()
self.estilo.sizeToFit()
Use autolayout
from the storyboard, press on a label and then in the right bottom side of the screen, you have the "Add new constrains" button.
Use it to set a spacing from a view to it's neighbors
https://developer.apple.com/library/tvos/documentation/UserExperience/Conceptual/AutolayoutPG/index.html

How to assign a view programmatically in ios

Let me explain my current scenario. I have a ViewController on storyboard as CenterViewController. On that view, I have the following properties-
#IBOutlet weak private var titleLabel: UILabel!
#IBOutlet var actualView: UIView!
I want to assign a view dynamically by program. For that I have a property i.e. -
#IBOutlet var actualView: UIView!
I have created a ViewController in storyboard. Now I need to assign the view that is related to that ViewController something like this-
let newView = ContactsViewController().view
actualView = newView
I know it will not work, but what else can I do for this purpose?
I think this will help you :
let newView = ContactsViewController()
actualView = newView.view

Resources