ios: Change the height of UIImageView with constraint inside UITableView - ios

in my app i am working on UITableView in which i am using UIImageView by setting up constraint on storyboard to display image. All i need is when there is no image available the height of ImageView will become 0 otherwise if image available height will be 150.

Try adding constraints to left, top, right. Add a IBOutlet in your viewController for NSContraint for height. And when initialising cell check for image is available or not. Depending on that set the IBOutlet value.
Add that IBOutlet to your height constraint of UIImageView.
Set TableView cell height to automaticDimensions.
Let me know if it helps or having any issue.

In storyboard
Set your imageView constraint height <= 150
in heightForR0w
return UITableViewAutomaticDimension
in estimateHeight
return 200

Give your ImageView to FixHeight Constraint
Create IBOutlet of that FixHeight Constraint
#IBOutlet weak var imgviewHeightConstraint: NSLayoutConstraint!
in your cellForRowAtIndexPath
check this way
if image available {
cell.imgviewHeightConstraint.constant = 150
} else {
cell.imgviewHeightConstraint.constant = 0
}

Related

How to set scrollview height programmatically

How can I set the scrollview height programmatically in Xcode 9 with Swift 4? I've set a constraint for the scrollview height and added that as an outlet. But when I change the outlet constant value programmatically it doesn't change the height.
Example:
#IBOutlet weak var scrollview_height: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollview_height.constant = 245
}
The height of the ScrollView should be given by it's content and not for you to change manually. At best if you want to do something like that, you have a view inside the scrollView and change the size of that view instead.
Step-by-step:
Add a ScrollView to your view. Set it's constraints (no height or width)
Add a View inside the ScrollView and set it's constraints. Additionally for a vertical scroll you might want to set it's width equal to the SuperView and ensure it has a height.
Create an IBOutlet for the height of the view
update the constant value of the constraint in code
If it does not update as expected, run a layoutIfNeeded on your superview after updates
You have to increase the content Size of your scrollView instead of increasing the frame of scrollView.
self.scrollview.contentSize = CGSize(width: screenWidth, height: yourDesiredHeight)
the scrollable contentSize of scrollView will change According to it.

Setting the height of UIlabel

I have a label that is in a custom uitableview cell. I am using auto layout to position the label. My question is how can one set the height of the label to zero when it does not have any text. Or how to to make the label height 0. The project supports ios 8 so using stackview is out of the question. The label is also expansive i can't set a constant height constraint.My layout is given below .
You can set label's height constraint >= 0. Label will update it's size to the content, and if text is missing height will be 0.
Another solution is possible that, set height constraint and take outlet of it and set it's constant to zero when text is nil.
Just ctrl + drag from height constraint to class to connect outlet to constraint.
then you can programatically change it's height when needed by changing it's constant.
for example,
#IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
and change constant like,
self.labelHeightConstraint.constant = 0
Hope this will help :)
In tableviewcell give height constraints except UILabel which one you need to hide.Change your Tableviewcell height it works.
For example, Your UILabel height is '30' and tableviewcell height is 130.You can give 130-30 = 100 in cell height.
//only sample code
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0
return 130
else
return 100
}
First set Height Constraint to Label. ctrl + drag the height Constraint to class to connect outlet to constraint. i.e
#IBOutlet weak var LabelHeightConstraint: NSLayoutConstraint!
and in implementation set the Height Constraint of Label to zero. As shown below....
self.LabelHeightConstraint.constant = 0;
if(check the label contains data)
{
self.LabelHeightConstraint.constant = 20;
}
else
{
self.LabelHeightConstraint.constant = 0;
}
This Logic will surely work......
Set the text as nil as and when required and set the content hugging priority high

Update constraints of other elements when UITableView height change

I have a UITableView of which I am defining height as 200 in autolayout. Based on that I am laying out other elements below it like UITextField etc. After that in run time I am fetching data from server and populating in UITableView due to which i am updating UITableView's height based on its content size. Following code I am using for it
self.myTableView.frame = CGRectMake(0 , 0, self.myTableView.frame.width, self.myTableView.contentSize.height)
But due to this, all the elements placed below UITableView still appear at same location which they were while laying out in Autolayout. Means change in height of UITableView makes no difference to them. Following image depicts this problem. What could be possible solution for this?
Here you can see, text fields are getting overlapped on tableview at run time. I am using Swift 2 in Xcode 7.2
If you have all required constraints to your table view and other view.
Don't change the frame of TableView to change height of it.
Instead create IBOutlet of height constraint of your tablview.
e.g. say IBOutlet name is constraintTableViewHeight,
then you can the the hight easily.
constraintTableViewHeight.constant = yourNewHeightValue
//update all constraint of your view and its inner view
self.view.layoutIfNeeded();
Refer Image to create IBOutlet for your height Constraint.
Take IBOutlet of NSLayoutConstraint for tableView Height and set its value not set tableview frame it's not working if you are already given constraints for tableview so change tableview hight constant
like if you take tblHeight for tableView height then set tblHeight.constant = self.myTableView.contentSize.height
You have to create an outlet connection for your table view height constraint:
#IBOutlet var heightConstraint: NSLayoutConstraint!
and when you want to change table view height, you can change the constraint value like this:
heightConstraint.constant = newHeightValue
self.view.layoutIfNeeded()

Moving UILabel dynamically based on UITextView contents - Using Storyboard

Using storyboard, I am trying to put a UILabel below UITextView. Content of UITextView can grow based on user input. I want my UILabel to reposition its Y value based on UITextView contents. I've tried fixing vertical spacing between UITextView and UILabel but that is not helping.
What else I shall be doing to crack this?
You should use constraints explicitly. The label in particular must have the constraint "top space to Textview" set to N points. Then set an outlet in your view controller from the constraint "top space to Textview" and change that constraint to resize uilabel Y position.
#IBOutlet weak var labelYConstraint: NSLayoutConstraint!
func changeLabelPosition(sender: AnyObject) {
labelYConstraint.constant = newY
}
Let me know if you need more details

How to change height of ImageView through nslayout?

I have ImageView. In Storyboard height of ImageView = 45. I need to change height = 70 programmatically. How can I do this?
Double click on the constraint to highlight it, make an IBOutlet of the constraint in your header file of your controller and change the constraint's constant value as you please.

Resources