0

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) 
Nitish
  • 13,845
  • 28
  • 135
  • 263
phadte viraj
  • 67
  • 2
  • 7
  • 1
    Possible duplication of - https://stackoverflow.com/questions/43010173/how-to-programmatically-increase-the-height-of-uiview-with-swift – Rashed Apr 11 '18 at 12:52
  • 5
    Possible duplicate of [How to programmatically increase the height of UIView with Swift](https://stackoverflow.com/questions/43010173/how-to-programmatically-increase-the-height-of-uiview-with-swift) – MRizwan33 Apr 11 '18 at 12:53
  • [How to update the constant height constraint of a UIView programatically?](https://stackoverflow.com/a/49776158/6822622) – AshvinGudaliya Oct 24 '18 at 07:11

5 Answers5

3

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.

Priya
  • 735
  • 5
  • 10
1

Create your constraint outlet and then set it like this :

  • self.heightConstraintOutlet.constant = newHeightValue
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
0

Approach

  • activate constraint
  • change constant value

Code

heightConstraint.isActive = true 
heightConstraint.constant = 20
Community
  • 1
  • 1
user1046037
  • 16,755
  • 12
  • 92
  • 138
0

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()
Nitish
  • 13,845
  • 28
  • 135
  • 263
0

// 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);
sara vss
  • 29
  • 5