phadte viraj
phadte viraj

Reputation: 77

How to update Height of the view Programmatically?

One issue i am facing related to autolayouts. I am setting height of view containing image views to zero first via autolayouts. But if certain function is called I want that height updated to a constant value, but height of my view is not getting updated. Here is the code, i have updated height programmatically inside the function but it is not working.

let heightContraint = NSLayoutConstraint(item: businessImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)
businessImageView.addConstraint(heightContraint) 

Upvotes: 0

Views: 7420

Answers (5)

sara vss
sara vss

Reputation: 29

// lets detect the height of our screen here

 int height = [UIScreen mainScreen].bounds.size.height;
    int width = [UIS

creen mainScreen].bounds.size.width;

// share the news


 NSString *message = [[NSString alloc]initWithFormat:@"This screen is \n\n%i pixels high and\n%i pixels wide", height, width];
    NSLog(@"%@", message);

Upvotes: 0

user1046037
user1046037

Reputation: 17685

Approach

  • activate constraint
  • change constant value

Code

heightConstraint.isActive = true 
heightConstraint.constant = 20

Upvotes: 0

Nitish
Nitish

Reputation: 14113

businessImageView.addConstraint(heightContraint) is not the code to update the constraint. It adds a constraint.
So as to update the height of parent view (which has images), you would need to update the constant for businessImageView's height constraint.

businessImageView.heightConstraint.constant = 40
self.view.layoutIfNeeded()

Upvotes: 0

Priya
Priya

Reputation: 749

First create IBOutlet of the height constraint.

You just need to change constant property of the constraint. For e.g.:

self.consTblFilterHeight.constant = 100.0
self.view.layoutIfNeeded() 

Replace self.view with the parent view of the view you are changing the height.

Upvotes: 3

Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Create your constraint outlet and then set it like this :

  • self.heightConstraintOutlet.constant = newHeightValue

Upvotes: 1

Related Questions