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
Related
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:
These are my codes
//My UIViews
#IBOutlet weak var UIVIewFirst: UIView!
#IBOutlet weak var UIViewSecond: UIView!
#IBOutlet weak var UIViewThird: UIView!
#IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var ViewThirdHeight: NSLayoutConstraint!
There is a button to show and hide the view as;
#IBAction func infoClicked(sender: SSRadioButton) {
if UIViewSecond.hidden {
sender.selected = false
UIViewSecond.hidden = false
self.middleViewHeightConstraint.constant = 134
} else {
sender.selected = true
UIViewSecond.hidden = true
self.middleViewHeightConstraint.constant = 0
self.ViewThirdHeight.constant = 180
}
}
the vertical gap between each view is 10. After hiding the view the gap becomes 20. But i need it to set it 10 between third and second view. Even though i set the third view height constant to any number it does not changes it position.Can anyone suggest why is this happening??
Constraints ignore the hidden property.
If possible for your requirements, embedd your views within a UIStackView.
See this example.
You need to take the outlet connection of the vertical spacing constraint between either First-Second or Second-Third views. Also if you want to hide/show only Second view, you don't need to do any changes to Third View height constraint.
Say For example, we take the outlet of vertical spacing between First and Second views, then:
#IBOutlet weak var UIVIewFirst: UIView!
#IBOutlet weak var UIViewSecond: UIView!
#IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var verticalSpacingConstraint: NSLayoutConstraint!
#IBAction func infoClicked(sender: UIButton)
{
if UIViewSecond.hidden
{
sender.selected = false
UIViewSecond.hidden = false
self.middleViewHeightConstraint.constant = 134
self.verticalSpacingConstraint.constant = 10
}
else
{
sender.selected = true
UIViewSecond.hidden = true
self.middleViewHeightConstraint.constant = 0
self.verticalSpacingConstraint.constant = 0
}
}
Below are the screenshots of output:
1. When button is not selected
2. When button is selected
You have not tell your view to update the view with the new constraint, you have to call this code:
self.view.layoutIfNeeded()
firstView
| gap = 10
secondView
| gap = 10
thirdView
after removing secondView
firstView
| gap = 10
---------- height = 0
| gap = 10
thirdView
so the gap becomes 20
try to add constraint programmatically after hiding the view or decrease any one gap.
Try to change the frame of the view instead of changing the constraint, also do view.layoutIfNeeded after making any changes.
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.
I'm trying to reorder an image and a stackview (here called labelsStack) that are both contained in another stackview (here called stack). My goald would be to programatically reverse the index order of both subviews in order to change their postition at runtime (they are horizontally distributed, so theorically, if I reorder their indexes, it should reorder them in autolayout)
I have tried to update indexes, exchange subviews, sendViewForward etc from the Apple doc, but it doesn t work, here s the code of my tableViewCell :
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func layoutSubviews() {
cellImage.layer.cornerRadius = cellImage.bounds.height / 3
cellImage.clipsToBounds = true
if incoming {
} else {
// as one of the many methods that didn't work
self.stack.insertSubview(cellImage, belowSubview: labelsStack)
}
}
You could create two alternative sets of constraints, one for the default order and the other for the inverse order. The default constraints are the one ending in 1
To initially deactivate the alternative setting you can use the inspector and change the installed checkbox
Your view controller should now have 6 outlets:
#IBOutlet weak var topViewTopConstraint1: NSLayoutConstraint!
#IBOutlet weak var bottomViewTopConstraint1: NSLayoutConstraint!
#IBOutlet weak var bottomViewBottomConstraint1: NSLayoutConstraint!
#IBOutlet weak var topViewTopConstraint2: NSLayoutConstraint!
#IBOutlet weak var bottomViewTopConstraint2: NSLayoutConstraint!
#IBOutlet weak var topViewBottomConstraint2: NSLayoutConstraint!
now to activate the other set of constraints you can use:
self.topViewBottomConstraint2.active = true
self.topViewTopConstraint2.active = true
self.bottomViewTopConstraint2.active = true
self.topViewTopConstraint1.active = false
self.bottomViewBottomConstraint1.active = false
self.bottomViewTopConstraint1.active = false
and viceversa
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