I'm trying to make my life easier by only building one xib to reuse rather than building three views to put into a stackView
I have created my custom xib file and connected it its own custom class:
The labels, and images are placed by constraints. I want the labels to be able to auto resize, up to three lines, so the height constraints for the labels are >=, as well as the Content View which also can scale in height (>=)
The UserReview code:
class UserReview: UIView {
#IBOutlet weak var userImage: UIImageView!
#IBOutlet weak var username: UILabel!
#IBOutlet weak var userRating: UILabel!
#IBOutlet weak var reviewText: UILabel!
#IBOutlet weak var prosText: UILabel!
#IBOutlet weak var consText: UILabel!
class func instanceFromNib() -> UIView {
return UINib(nibName: "UserReview", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
func setUp(review: Review){
// TODO set userImage
username.text = review.username!
// TODO add rating + color
reviewText.text = review.content!
if let pros = review.positive_points {
prosText.text = pros
} else {
prosText.text = "No Positive Points"
}
if let cons = review.negative_points {
consText.text = cons
} else {
consText.text = "No Negative Points"
}
}
}
The StackView where i am trying to add subViews to also has a height constraint of >= 200 (min one card) but does not seem to resize with more cards added. The Alignment is Fill and Distribution: Equal Spacing
I have tried playing around with the constraints and alignment properties for the StackView containing reviews but the closest i can come to is overlapping Views..
The way i create and add views to the stack view:
let review1 = UserReview.instanceFromNib() as! UserReview
let review2 = UserReview.instanceFromNib() as! UserReview
let review3 = UserReview.instanceFromNib() as! UserReview
self.reviewStack.addArrangedSubview(review1)
self.reviewStack.addArrangedSubview(review2)
self.reviewStack.addArrangedSubview(review3)
From what i understand from this stack question
The StackView has issues with the view height?
I have tried most of the suggested solutions but encountering different issues..
The result i am trying to get:
(The images are gone for some reason, will check that later.)
First of all remove height constraint from UIStackView because StackView always expands it's height based on the views inside it.
Set all the 4 sides constraints of StackView to it's superview and set intrinsic size as place holder.
Now add your UIView to stack view in the same manner you are doing it now. Do not set height constraint to any UIView label and the UIView itself. Just make sure yo have set top constraint of top most element and bottom constraint of bottom element. This ways auto layout understand how to increase height of all the elements based on their constraints and content.
For detailed understanding you can follow my answer at https://stackoverflow.com/a/57954517/3339966
Related
I have a self sizing UITableViewCell that displays blog posts.
A post consists of text and an image. The challenge I now have is that some posts do not contain an image.
How do I size the cell using Auto Layout? Perhaps should I set the UIImageView.isHidden = true instead?
Thanks!
Considering you said you are using auto resizing cells, following are the steps you should follow:
1) First subclass your tableview cell with custom UITableViewCell class
2) Take an outlet for UIImageView in above class as:
#IBOutlet weak var myImageView: UIImageView
3) Set the auto layout constraints to this image view from storyboard as leading, trailing, top and height constraint
4) Take outlet for height constraint of imageview in the same UITableViewCell class:
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
5) In your cellForRow method, check if your data contains image or not. If no image is present, then change the value of height constraint:
if myImage == nil {
if myCustomCell.heightConstraint != nil {
myCustomCell.heightConstraint.constant = 0
myCustomCell.updateConstraintsIfNeeded()
}
}
This should work as expected. If not, please post the constraints you have applied.
I am trying to have a UIImageView (centered horizontally in the viewController) with a UISwitch inside of it.
When the UIswitch is turned to the ON position - a second UIImageView should appear to the left of the original UIImageView, while the constraints should make it so that both UIImageViews are centered horizontally.
thanks in advance
There are many ways to do this, but the easiest is to simply use a centered horizontal UIStackView that contains only a single imageView. When the switch is flipped add the second imageView to the stackView by calling insertArrangedSubview(:at:) and the stackView will take care of maintaining the centering and any requested spacing. Similarly when the switch is off just call removeArrangedSubview(:) and everything goes back into place.
If you are not on iOS 9 and don't have UIStackView you can just drag an IBOutlet to the view that you want to move's centerX constraint and add to its .constant property and animate layoutIfNeeded to have it slide to the right.
class ViewController: UIViewController{
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var imageViewCenterXConstraint: NSLayoutConstraint!
let padding = CGFloat(10)
#IBAction func tapButton(_ sender: Any) {
imageViewCenterXConstraint.constant = imageView.frame.size.width / 2 + padding
UIView.animate(withDuration: 0.25) { self.view.layoutIfNeeded()}
}
}
I have the structures below...
I wrap two of collection views into tableview
One is in tableview header(Collection1), another is in tableview 1st row(Collection2).
All the functions are good (Both collection view).
just...
When I scroll up in Collection2, Collection1 will Not scroll up together, because I'm only scrolling the collectionViews not the tableview.
It only scroll together when I scroll in Collection1.
Is it possible to make the header view scroll with user just like app store's index carousel header?
Or I just went to the wrong place, I should use other ways to approach.
Solution
When you keep CollectionView1 as a TableViewHeader, CollectionView1 will always on the top of TableView after it reaches top. If you want Collection1 and Collection2 scroll up together, you need to keep CollectionView1 in a cell, not a header.
Make sure CollectionView2 content height smaller or equal to TableViewCell's height. As I checked on App Store, they always make SubCollectionView content height equal to TableViewCell's height (If they use TableView).
Result
For more detail, you can take a look at my sample project
https://github.com/trungducc/stackoverflow/tree/app-store-header
Problem
1) Your tableview cell Collectionview (let's say collection2) , Collection 2 is scrollable. So when you scroll up tableview won't scroll up
Solution
1) Just simple and working solution would be height constant , You have to give height constant to the collection2 with >= relationship and 0 Constant value and 750 priority !!
Now the question is how to use this
You need to take IBOutlet of Height constant to your custom tableview cell and need to manage the collectionview height from there.
Here is example
class FooTableViewCell: UITableViewCell {
static let singleCellHeight = 88;
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var iconsCollectionView: IconsCollectionView!
#IBOutlet weak var const_Height_CollectionView: NSLayoutConstraint!
var delegateCollection : TableViewDelegate?
var bars:[Bar] = [] {
didSet {
self.iconsCollectionView.reloadData()
iconsCollectionView.setNeedsLayout()
self.layoutIfNeeded()
let items:CGFloat = CGFloat(bars.count + 1)
let value = (items / 3.0).rounded(.awayFromZero)
const_Height_CollectionView.constant = CGFloat((iconsCollectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize.height * value)
self.layoutIfNeeded()
}
}
override func awakeFromNib() {
iconsCollectionView.translatesAutoresizingMaskIntoConstraints = false
iconsCollectionView.initFlowLayout(superviewWidth: self.frame.width)
iconsCollectionView.setNeedsLayout()
iconsCollectionView.dataSource = self
iconsCollectionView.delegate = self
const_Height_CollectionView.constant = iconsCollectionView.contentSize.height
self.layoutIfNeeded()
self.setNeedsLayout()
}
}
You can check my answer Making UITableView with embedded UICollectionView using UITableViewAutomaticDimension Very similar and 100% working
Another solution You can also take UICollectionView with sections which contain another horizontal collectionview , with this solution you don't need to manage contentsize for every cell
Hope it is helpful
I'm trying to allow the height of the subview (the white box inside the view. The view controller swift file is a separate XIB file) depending on the amount of content in it. How do I do this?
This is what I have so far for it:
#IBOutlet weak var myScrollView: UIScrollView!
#IBOutlet weak var myContentView: UIView!
override func viewDidLayoutSubviews()
{
let scrollViewBounds = myScrollView.bounds
let containerViewBounds = //I am not sure how to do the rest
}
The best way to achieve this is to use autolayout. You have great tutorail here:
http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
If you don't want to use it, set reference for your view inside your view controller for that view, and use various functions to calculate height. There are several ways for labels ( Adjust UILabel height to text ), text field( How to set UITextField height? ), etc.
I have build a custom table cell:
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var streetLabel: UILabel!
#IBOutlet weak var cityLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
func loadItem(#street: String, city: String, price: String){
self.streetLabel.text = street
self.cityLabel.text = city
self.priceLabel.text = price
}
}
Result on iPhone 5:
Result on iPhone 6+
As you can see the left side of the cell is displayed correctly, it sticks to the left. However the right side should stick to right but it does not. So here is my question:
How can the width of the cell be set to the width of the tableView so that the cell has the same width as the screen?
How can I make the right side actually stick to the right of the table cell?
UITableViewCells are always the with of the UITableView they are in so there is noting that you need to do.
Based on the screenshot you posted, it doesn't look like you are currently using autolayout to position your labels. What you'll need to do is just add a constraint from the right of your price label to the right of the cell. Then when the cell grows, your label will keep the same distance from the right edge of the cell.
Your constraints for the top labels should then look like this.