How to increase UIView height programmatically with Swift

Inside IB, I have a viewController with a segmentedControl and 2 containerViews. In each ViewView container, I have a tableView. From each tableView, I click on the detailView. My auto layout in IB.

As soon as I select a segment, I see the corresponding tableView, I select a cell and the correct detailView appears in the scene.

The problem is that when the detailView is placed in the segmentedControl it is still visible. I decided to hide the segmented control, which works, but there is a large white space in there. I tried to programmatically increase the size of the detailView but it didn't expand. From what I read because I made the initial constraints in the storyboard.

How do I programmatically get a detailView for an extension?

code:

DetailViewController: UIViewController{

override func viewDidLoad() {
        super.viewDidLoad()

 //this isn't increasing the view 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)

      

The left side of the mutation operator is not mutable: the 'height' property is get-only

//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)

      

Unable to call initializer for type 'CGRect' with argument list of type '(x: Int, y: Int, width: CGFloat, height :())'

+13


source to share


4 answers


You need to create a rosette to limit the height of the detail view, and then you can adjust the height programmatically as shown in the figure myHeightConstraint.constant += extraHeight

, where extraHeight

is a number that indicates how high you want the detail view to be. You may also need to call self.view.layoutIfNeeded()

after.

To create an exit from the constraint, drag it from the constraint in the storyboard editor (just like you did for the UIView exit) into your code.



You are correct - because you are using automatic layout and constraints, adjustments also need to be constrained. Placing raw frames can lead to unexpected behavior. Let me know if you have any other questions or concerns.

+24


source


Inside an initializer, CGRect

you shouldn't be using +=

, just +

.

Edit:

self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)

      



in

self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + 20.0)

      

You shouldn't try to change the value self.view.frame.height

inside the initializer.

+10


source


Are there any height restrictions for the segmentedControl, if possible you can remove them when loading the verbose view

0


source


Use self.view.frame.size.height = 80

-1


source







All Articles