Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically increase the height of UIView with Swift

Inside IB I have a viewController with a segmentedControl and 2 containerViews. In each containerView I have a tableView. From each tableView I push on a detailView. My auto layout is in IB.

Once I pick a segment, I see the corresponding tableView, I pick a cell and the correct detailView gets pushed on the scene.

The issue is once the detailView gets pushed on the segmentedControl is still visible. I decided to hide the segmentedControl which works but there is a big empty space there. I tried to programmatically increase the size of detailView's view but it's not expanding. From what I read it's because I made the initial constraints in storyboards.

How to I programmatically get the detailView's view to expand?

code:

DetailViewController: UIViewController{  override func viewDidLoad() {         super.viewDidLoad()   //this isn't increasing the view's size  self.view.frame.size.height += 20.0   //doesn't work. I get an error on the last argument  self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)   //doesn't work. I get an error on the last argument  self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)  }  } 

Errors:

//Error for the last argument- height: *self.view.frame.height += 20.0* self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0) 

Left side of mutating operator isn't mutable: 'height' is a get-only property

//Error for the last argument- *self.view.frame.size.height += 20.0*: self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0) 

Cannot invoke initializer for type 'CGRect' with an argument list of type '(x: Int, y: Int, width: CGFloat, height: ())'

like image 886
Lance Samaria Avatar asked Mar 24 '17 22:03

Lance Samaria


People also ask

How do you change the size of programmatically in UIView?

size. width = 200; newFrame. size. height = 200; [self setFrame:newFrame]; CGRect newFrame = self.

How do I show height in Swift programmatically?

To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height.

What is UIView in Swift?

The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.


1 Answers

You need to create an outlet for the height constraint of the detail view, and then you can adjust the height programmatically like this myHeightConstraint.constant += extraHeight where extraHeight is a number indicating how much taller you want the detail view to be. You also may need to call self.view.layoutIfNeeded() afterwards.

To create an outlet from a constraint, control drag from the constraint in the storyboard editor (just like you would for an outlet of a UIView) into your code.

You are right - because you are using auto layout and constraints, the adjustments need to be made with constraints too. Setting the raw frames can lead to unexpected behavior. Let me know if you have any other questions or difficulties.

like image 152
CoolPenguin Avatar answered Sep 17 '22 12:09

CoolPenguin