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
Related
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 am trying to create round corners for a UIView with in a custom TableViewCell. The problem is when the table view loads, it only rounds the Top Left corner of the view and not the bottom one. Now when i scroll the table view little bit, it rounds the bottom left part of the view as well.
I have tried every possible method but i can't get my head around it. I am also attaching the screenshots as well as copying the code. Thanks
The code that i am using in the custom view cell class is below.
class FoodTVCell: UITableViewCell {
var food: Food!
#IBOutlet weak var foodPicture: UIImageView!
#IBOutlet weak var foodName: UILabel!
#IBOutlet weak var foodRating: UIImageView!
#IBOutlet weak var deliveryTime: UILabel!
#IBOutlet weak var minOrder: UILabel!
#IBOutlet weak var category: UILabel!
#IBOutlet weak var foodPriceLabelBG: UIView!
#IBOutlet weak var foodPrice: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
foodPicture.layer.cornerRadius = 75/2
foodPicture.clipsToBounds = true
}
override func layoutSubviews() {
super.layoutSubviews()
let maskLayer = CAShapeLayer()
let corners = UIRectCorner.TopLeft.union(UIRectCorner.BottomLeft)
maskLayer.path = UIBezierPath(roundedRect: foodPriceLabelBG.bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(20.0, 20.0)).CGPath
foodPriceLabelBG.layer.mask = maskLayer
foodPriceLabelBG.clipsToBounds = true
}
}
Your 'corner rounding' code is working very well, the problem is that your UIView's bottom edge is going outside of your Cell's bottom edge. Decrease your UIView's Y position and it will be covered by your Cell. I see your cell's separator line invisible too, that means you should give larger height to your Cell at heightForRowAtIndexPath:
You said it's working very well on scrolling, I suppose it's working when scrolling down and not working when scrolling to top. It means that your first cell was redrawn after second while scrolling to the bottom (where the second cell doesn't cover your first cell), and vice versa while scrolling to the top (where second cell covers your first cell)
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 have a big paragraph i want to add to a view. But the problem i am having is the first textview is small and then under it i have a map view. After the map view i want the text to continue from where it stopped in the first textview. I put an example below to show exactly what i mean.
#IBOutlet var firstSection: UITextView!
#IBOutlet var articleMapView: MKMapView!
#IBOutlet var secondSection: UITextView!
I want have a text above to map and and below it.
I am new to swift and am trying to add a UIScrollview to a custom UITableViewCell. I can't seem to get the scrollview to scroll in the simulator though. I have tested both vertical and horizontal scrolling.
Here is the code for my custom UITableViewCell
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var winLoseValueLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var newTotalLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var playersLabel: UILabel!
#IBOutlet weak var playersScrollView: UIScrollView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersLabel.text = nil
//self.playersScrollView.contentSize.height = 1000
self.playersScrollView.contentSize.width = 1000
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
if self.playersLabel.text == nil{
self.playersLabel.text = str
}
else{
self.playersLabel?.text = self.playersLabel.text! + "\n\(str)"
}
}
}
My playersScrollView outlet has been properly connected to the scrollview in my storyboard. As far as I can see from all of the tutorials and sources online, I should just have to set the contentSize width or height (larger than the view area) to get it to scroll, which I have done in the awakeFromNib function.
Any ideas why this is not working in the simulator? Secondly, ideally I would like to have a vertical scrolling scrollview in the table cell, but I thought that the table view's vertical scrolling might have been interfering with the scrollview, so I changed it for horizontal scrolling for testing, which also didn't work.
Can you have a vertical scrollview in a vertically scrolling UITableView, or will the table view scrolling interfere too much with the scrollview?
Ok, it turns out that the constraints on my playersLabel, which is a subview of playersScrollView, was the culprit, although I am not sure why. I adjusted the constraints on the label and that allowed the horizontal scrolling to work.
The vertical scrolling still did not work, so I had to override the touchesBegan and touchesEnded methods in the scroll view so I could disable/re-enable the scrolling of the table view.