I have a custom cell that has an image and a label in it next to each other. I am reusing the custom cell in another place but I have a condition that hides the image.
How do I set the label to be centered after hiding the image?
I tried to set the text alignment to center but it didn't work.
if (condition) == false{
self.checkImage.isHidden = true
}
else {
self.checkImage.isHidden = false
}
An easy option is to put both the imageView and the label inside a horizontal stackView which will make the label centered when you set isHidden to the imageView
Or
Make an outlet of the imageView width and set it's constant to zero
Related
I have a UITableViewCell with a vertical UIStackView that currently has .alignment = fill and distribution = fill. Inside the stack view, I have a UILabel and a UIImageView.
The label should be left aligned and stretch across the width of the stackview (and it does). The image view has a content mode of aspect fit and should be centered in the middle of the stackview. I thought this was working as I wanted it to until I set a background color on the UIImageView and then realized that while the image itself appears correct (scaled and centered) the view itself stretches across the full width of the stackview (just like the label does).
Is it possible to have the label stretch the width of the stack view but not the image view? Ideally, I'd like the image view to just be the size of the scaled image and not stretch all the way to the edges of the stack view.
Make these changes:
Set your stack view's alignment to .center instead of .fill.
Constrain the label's width to equal the stack view's width.
In code, when you set the image view's image, also create an aspect-ratio constraint on the image view, like this:
class MyCell: UITableViewCell {
#IBOutlet private var myImageView: UIImageView!
private var imageViewAspectConstraint: NSLayoutConstraint?
func setImage(_ image: UIImage) {
myImageView.image = image
imageViewAspectConstraint?.isActive = false
imageViewAspectConstraint = myImageView.widthAnchor.constraint(
equalTo: myImageView.heightAnchor,
multiplier: image.size.width / image.size.height)
imageViewAspectConstraint!.isActive = true
}
}
Note that, since cells can be reused, you also have to remove a prior aspect-ratio constraint if there is one. The code I posted uses imageViewAspectConstraint to save the constraint and remove it when the image changes.
One approach:
calculate appropriate aspect-fit frame for your image
When the user taps on the image view, evaluate the tap position and only take action if the tap is within the aspect-fit frame.
Another approach:
calculate appropriate aspect-fit frame for your image
embed the UIImageView horizontally centered inside a UILayoutGuide
add the UILayoutGuide as the stack view's arranged subview
This will keep your label stretched across the width of the stack view (the cell) and center your image view, allowing you to detect the taps.
Hi I'm new to UIStackViews.
So I have a vertical Stackview containing 8 Labels.
However, the problem with my Stackview is that whenever I hide the Labels inside it, my stackview does not resize.
The labels within the Red Rectangle are in my Stackview
When I hide those labels of the Stackview, I want my screen to look like this
However for some reason when I hide those labels, it looks like this instead with all the space visible and the stuff at the bottom doesn't go up
How do I fix this?
Here's how I hid the labels:
override func viewDidLoad() {
super.viewDidLoad()
showingResultLabel.isHidden = true
addressLabel.isHidden = true
costOfKitLabel.isHidden = true
costOfModularLabel.isHidden = true
dispatchedFromLabel.isHidden = true
kitHomecostLabel.isHidden = true
modularHomecostLabel.isHidden = true
dispatchFromLabel.isHidden = true
Thanks
Just hide the labels works. You need to set constraints to make it work. I upload images of constraints.
stackview's constraint
hiding a label inside stack view does not effect the height.
I suggest that, when you want to show some detail (stackView contain some UILabel),
create the UILabel e.g:
let label = UILabel()
label.textColor = .black
label.text = ""
label.backgroundColor = .clear
then add the UILabelView to the stack view
stackViewName.addArrangedSubview(label)
at the end, instead of hide a label inside stack view, you have to remove the UILabel from the stack view
stackView.removeArrangedSubview(label)
so i think this will effect the hight of your stackView .
I have a UITableView and have cells in it and I am using textLabel and detailTextLabel in it along with a accessoryView which is a disclosureIndicator. These are my constraints.
if (cell.contentView.constraints.count == 0) {
cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
cell.detailTextLabel?.translatesAutoresizingMaskIntoConstraints = false
let margins = cell.contentView.layoutMarginsGuide
// TextLabel constraints
cell.textLabel?.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
cell.textLabel?.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
// DetailTextLabel constraints
cell.detailTextLabel?.rightAnchor.constraint(equalTo: margins.rightAnchor, constant: -10).isActive = true
cell.detailTextLabel?.leftAnchor.constraint(equalTo: (cell.textLabel?.layoutMarginsGuide.rightAnchor)!, constant: 10).isActive = true
cell.detailTextLabel?.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
}
Now when the Table View is first loaded I see everything fine i.e detailTextLabel is -10 points away from contentView but when I tap the cell the detailTextLabel shifts to the right and hugs to the contentView as if -10 point constraint isn't even there. Now when I come back to this VC again, I still see detailTextLabel hugging to contentView i.e reloadData is not taking any effect. But if I dismiss this VC and come back again here my constraint and in place like they should be and the problem comes up again when I tap on the cell to go to the next screen.
I have a reloadData() call in my viewWillAppear which gets called but doesn't produce any effect.
I hope I am able to explain myself, am I doing anything wrong here?
The problem is that you are using a built in cell type, where the text label and detail label are built in and don’t belong to you. They belong to the cell. The cell repositions them on selection. You are fighting the cell.
If you want to customize a cell’s appearance and change the position of the cell labels, use a custom cell type, and give it custom labels that belong to you. You can given them constraints right in the storyboard designer. Do not use the built in text label and detail label at all.
You cannot change the constraints of the cell labels if it is not a custom type cell. If you want custom constraints then you should create your own custom cell which inherits from UITableViewCell and set your own constraints.
I have button and a label in my table view cell placing one after other vertically.
In my xib design i placed my button then below it placed my label.
if the string for label is empty then i want to set the button as vertically center in table view cell and hide the label.
if have tried following code for my implementation but button does not come in vertical center position.
if([labelString isEqualToString:#""]){
_label.hidden = YES;
_button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
it would be helpful if get suggestion for fixing this problem.
One approach: use dynamic setting of constraints.
In InterfaceBuilder:
Put your button and label inside an enclosing wrapper view.
Constrain the wrapper view to be centered in the main view.
Constrain the button and the label to be of fixed height, and the top of the label to be constrained to the bottom of the button (with spacing if desired).
Constrain the top of the wrapper to the top of the button.
Constrain the bottom of the wrapper with two separate constraints: one to the bottom of the button (but make this inactive), and one to the bottom of the label (active.).
Make both of those constraints outlets so that you can access them in code.
Leave the wrapper height otherwise unconstrained. Thus it will "shrink-wrap" to the combined height of the button and label.
In code:
When the label is empty, activate the constraint that pins the wrapper to the button instead of to the label:
BOOL hideLabel = [labelString isEqualToString:#""];
_label.hidden = hideLabel;
_wrapperToButtonConstraint.active = hideLabel;
_wrapperToLabelConstraint.active = !hideLabel;
Now the shrink-wrap effect will make the wrapper have the same height as the button, and since the wrapper is centered, so will the button be centered.
I'm trying to load a image to my tableview cell, however, the image seems to be bigger than the imageview and exceed the boundary of imageview.
I tried to set the contentMode property of the UIImageView to ScaleAspectFit but it didn't solve my problem.
Code :
cell.imageView?.contentMode = UIViewContentMode.Center
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
switch (post.priorityLevel){
case "High Priority Posting":
cell.imageView?.image = UIImage(named: "High Priority Posting")
case "Priority Posting":
cell.imageView?.image = UIImage(named: "Priority Posting")
case "General Posting":
cell.imageView?.image = UIImage(named: "General Posting")
default:
print("Priority Level can't be loaded to table")
}
I added screenshots of my table view from Interface builder and the result on simulator.
Any suggestions will be appreciated.
Follow below constraint and you will see the magic of Auto layouts
ImageView constraints:
Set the fix height and width you want to display for the imageView, and set VerticalCentre constraint. Set imageView's content mode to Aspect Fit.
Top label constraints:
Label TrailingSpace constraint should not be a constant value, instead set it to standard spacing (i.e 8 pts).
Second label constraints:
Labels TrailingSpace and BottomSpace constraint should not be a constant value, instead set it to standard spacing (i.e 8 pts), and set its VerticalContentHugging priority to 250
Update:
ImageView vertical constraint to cell:
Label1 TrailingSpace to cell with standard spacing:
Label2 TrailingSpace to cell with standard spacing and BottomSpace to cell with standard spacing.
Try cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson7.html#//apple_ref/doc/uid/TP40015214-CH8-SW1
In this official tutorial, they design a tableview exactly like yours. I think your main issue has to do with the lack of constraints/autolayout
UIImage
1) first you set the imageview leading space ,equal height,center vertically to container
2)set height itself
City and State
1) set horizontal space and Top to UiimageView, Trailing space to container view
2)set height itself
Description label
1)set horizontal space and Bottom to UiimageView, Trailing space to container view
2)set height itself
story board -> set lines = 0
If its a custom table view cell, dont name the imageView outlet to "imageView", name it to any other for eg: "cellImageView". and then try setting clipsToBound to true of your cell cellImageView.