UITextView height won't change - ios

I don't know why, but when I try to change the height of a UITextView there is no change at all.
CGRect frame = self.descView.frame;
frame.size.height = 500;
self.descView.frame = frame;
I have created the UITextView from storyboard and I used fixed height.
What am I doing wrong?

You can change your textview height using create an instance of height constraint of textview.
from storyboard first right click on height constraint of textview and create instance in class file like below
#IBOutlet var textviewHeight: NSLayoutConstraint!
then, easily you can play with its height
textviewHeight.constant = 500
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}

If there are no autolayout conflicts the following code should set the correct height.
self.descView.heightAnchor.constraint(equalToConstant: 500).isActive = true

Related

Animate subview AutoLayout constraint changes

Numerous tutorials on animating AutoLayout constraints suggest to update constant property of a constraint and then call layoutIfNeeded() in animation block.
My situation is a bit tricky.
I have a view that houses 3 subviews. The height of this superview is not fixed - it is calculated as a sum of heights of its subviews.
On some event, I ask one of those 3 subviews to toggle its height (it changes between 0 and 30, i.e. I want to smoothly hide and show it).
The code is similar to this:
// In superview
subview1.isVisibleInContext = !subview1.isVisibleInContext
// In subview
class MySubview: UIView {
#IBOutlet var heightConstraint: NSLayoutConstraint!
var isVisibleInContext = false {
didSet {
updateHeight()
}
}
func toggleHeight() {
let newHeight = isVisibleInContext ? 30 : 0
layoutIfNeeded()
heightConstraint.constant = newHeight
UIView.animate(withDuration: 0.8) {
self.layoutIfNeeded()
}
}
}
Unfortunately, this does not work as I expect.
I can see the smooth change of the height of my subview, but the height of my superview is recalculated immediately after I set the new value for my subview height constraint.
I want to see the height of the superview gradually increasing/decreasing as on of its subviews grows or decreases.
Please someone point me in the right direction. Any help is appreciated.
Thanks a lot!
The animation block should be in the UIView that contains the 3 MySubviews. Inside the MySubview class you only update the height constraint's constant:
In Subview
func toggleHeight() {
let newHeight = isVisibleInContext ? 30 : 0
heightConstraint.constant = newHeight
}
Then in the UIView that contains the 3 MySubviews you animate the change:
In Superview
func toggleHeight(with subview: MySubview) {
subview.toggleHeight()
UIView.animate(withDuration: 0.8) {
self.view.layoutIfNeeded()
}
}
The first thing, that was incorrect in my approach was executing self.layoutIfNeeded(). Having investigated the issue I learned out that I had to execute it on the superivew, like this:
self.superview?.layoutIfNeeded()
That also didn't work out for a reason. The main issue in this case was that the view, which had 3 subviews inside was itself a subview of view. So to make it work I had to use the following code:
self.superview?.superview?.layoutIfNeeded()
Definitely not good that subview has to know the hierarchy of views, however it works.

Autolayout working wrong

I'm working on profile screen, there is a UITableView inside the ViewController and I placed all user info inside the UITableView Header. To make screen more accurate due to different sizes of "About" label I use Autolayout with this code:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
sizeHeaderToFit()
}
func sizeHeaderToFit() {
let headerView = tableView.tableHeaderView!
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame
tableView.tableHeaderView = headerView
}
Everything works fine, but the last string of About label is not showing, text interrupts (I set word wrap):
Add a height constraint on tableview header and try to change height constraint where you are changing frame. As you are using autolayout so you should play with the constraints rather than playing directly with the frames
you can follow this tutorial
https://useyourloaf.com/blog/variable-height-table-view-header/
Hope it will help you.

How can I update a UIView's position and size in reference to it's superview's position and size? [Swift]

I have a superview with another subview inside of it. The subview has constraints set to it to be centered in the superview and half the size of the superview.
The position and size of the superview are being changed when a button is clicked, but the position and size of the subview is not being changed with it. I tried using UIView.updateConstraints(), but it is not repositioning it or resizing it.
So my question is:
What would be the best way to resize and reposition the subview with respect to the superview?
Thank you!
Here is the code:
func updateTimerFormat() {
minutesContainer.frame.size.width *= 0.75
minutesContainer.frame.size.height *= 0.75
minutesContainer.center.x = view.center.x
minutesContainer.center.y = view.center.y * 1.5 - 30
// The minutes label is the subview of the minutesContainer
minutesLabel.updateConstraints()
}
You should not to call updateConstraints(), because I do not think your view's constraints need to be updated.
You just need to change SuperView's size or position. Then your view can automatically set the size and position, because you set their constraints before.
This is a demo.https://github.com/cythb/iOSIssues/tree/master/2_AjustSizeDemo
Constraint subview(yellow) center to superview(green) center. Constraint size is half of the superview(green).
When you click button, you don't do anything else,just change the size of superview.
There are two ways-
Implement the func layoutSubviews(). This method is called when super view frame become change. In this method you can change your subviews frame.
Second option is that use autolayout during configure the subviews
1/ Connect minutesContainer constraints to your ViewController:
#IBOutlet weak var widthConstraint: NSLayoutConstraint!
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
#IBOutlet weak var centerYConstraint: NSLayoutConstraint!
2/ Change your updateTimerFormat method:
func updateTimerFormat() {
widthConstraint.constant *= 0.75
heightConstraint.constant *= 0.75
centerYConstraint.constant = view.center.y * 0.5 - 30
}

Chaning height of UIView

I'm trying to change the height and width of UIView using Swift and it's not working. Tried both codes below, still no progress.
self.contentView.frame.size.height = 100
self.contentView.bounds.size.height = 100
contentView is a subview of the main view.
I guess you are using constraints. And when you use constraints you should change not the frame but constraints itself. It should looks like this:
In storyboard, pick height constraint for your view and create outlet to controller:
And then change it's constant value:
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
func changeHeight() {
heightConstraint.constant = 200
// uncomment to perform changes with animation
// UIView.animateWithDuration(0.3) { () -> Void in
self.view.layoutIfNeeded()
// }
}

How to resize UITableView width to fit its content?

Is there possible way to adjust table view width by width of biggest text that should be set in the cellTitle label?
iOS 8 , SWIFT
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Set the popover tableview width to be max compressed size (assumes all cells visible)
let maxWidth = tableView.visibleCells.reduce(0.0) { (currentMax, cell) -> CGFloat in
return max(currentMax, cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).width)
}
var contentSize = tableView.contentSize
contentSize.width = maxWidth
tableView.contentSize = contentSize
tableView.bounds.size = contentSize
}
Set a width auto-layout constraint to the table view.
Set an outlet from the constraint to your code (ctrl+drag, just like a view).
When you have the size of the label (if the label also has auto-layout then you will have it at viewDidLayoutSubviews) set the width to the table like so:
- (void)viewDidLayoutSubviews {
self.myTableWidthConstraint.constant = self.myLabel.frame.size.width;
[self.myTableView updateConstraints];
[self.myTableView layoutIfNeeded];
}
You should be able to do that in the size inspector.

Resources