How to update the constant height constraint of a UIView programmatically?
Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.
yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()
Method updateConstraints()
is an instance method of UIView
. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.
Update height constraint programmatically
Instead of adding a new constraint, you need to modify the constant on your existing constraint.
Use an IBOutlet to connect to your constraint in Interface Builder:
@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;
Then, when you need to set it programmatically, simply set the constant property on the constraint:
heightConstraint.constant = 100;
OR
If you can't access the nib in Interface Builder, find the constraint in code:
NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in myView.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
heightConstraint = constraint;
break;
}
}
heightConstraint.constant = 100;
And in Swift:
if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {
constraint.constant = 100.0
}
Swift UIView Constraint Constant Height Changing Time Getting LayoutConstraints Warning
Below working code for adjusting constraints constant height
// to update height constant
photoGalleryView.updateConstraint(attribute: NSLayoutAttribute.height, constant: 20.0)
extension UIView {
func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
constraint.constant = constant
self.layoutIfNeeded()
}
}
}
Layout is Broken when I update the Table View Height Constraint programmatically
You can try one thing -> change tableview height constraint priority to 100 and try to remove all fixed height of cell.
Hope it'll help you.
Try this one.It's work perfectly for me.
let cellHeight = 100.0
var cell : tableCell?
let detailArr : [UIColor] = [.red,.yellow,.black,.blue]
override func viewWillAppear(_ animated: Bool) {
self.changeTableHeight()
}
func changeTableHeight()
{
let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
animator.addAnimations {
self.view.layoutIfNeeded()
}
tableviewHeight.constant = CGFloat(Double(detailArr.count) * self.cellHeight)
self.view.setNeedsLayout()
animator.startAnimation()
self.tableview.reloadData()
}
UIView does not update new height constraint after action
You need to remove old 1
extension UIView {
func constrainHeight(constant: CGFloat) {
constraints.forEach {
if $0.firstAttribute == .height {
self.removeConstraint($0)
}
}
heightAnchor.constraint(equalToConstant: constant).isActive = true
superView!.layoutIfNeeded()
}
}
Update layout constraints programmatically without IBOutlets - Swift
Store the constraint in a variable and change the constant and call layoutIfNeeded
when you need it animated.
// Declare this along with the other variables in your class
var constraintVariable: NSLayoutConstraint!
.
.
.
// Where you set your constraints. Store the constraint to be animated in the variable and make it active
// Your other constraints
constraintVariable = sideMenu.leftAnchor.constraint(equalTo: view.leftAnchor, constant: someNegativeValue);
constraintVariable.isActive = true
.
.
.
@objc func slideMenu() {
UIView.animate(withDuration: suitableDuration) {
constraintVariable.constant = requiredValue
view.setNeedsLayout()
view.layoutIfNeeded()
}
}
Related Topics
Input from the Keyboard in Command Line Application
Non-'@Objc' Method Does Not Satisfy Optional Requirement of '@Objc' Protocol
Dynamically Hiding View in Swiftui
How to Hash Nsstring With Sha1 in Swift
How to Unwrap an Optional Value from Any Type
Initialize @Stateobject With a Parameter in Swiftui
How to Use Attributed String in Swiftui
How to Stop a Dispatchworkitem in Gcd
Deleting List Elements from Swiftui'S List
How to Compare One Value Against Multiple Values - Swift
Differences in Nsdatecomponents Syntax
How to Generate a Random Number in a Range (10...20) Using Swift
How to Display an Activity Indicator With Text on iOS 8 With Swift