Cue
Cue

Reputation: 3092

Resizing a UITextView programmatically in Swift 2 to enlarge its width

EDIT: I post a screenshot of what I would like to implement in iPad and iPhone. When using the button in iPhone, the UITableView opens and has to cover a part of the UITextView.

enter image description here

I have a universal project for iPhone and iPad. In the main storyboard there are two controls, an UITextField and a UITableView. The UITextField keeps the left 1/3 portion of the screen and the UITableView the remaining part of the screen.

When in iPhone, I hide the UITableView and I would like that the UITextView keeps all the screen. Then, when the user taps on a button, the UITableView appears over the text field (I dismiss the UITableView as soon as the user selects a choice).

How can I have the tableView to enlarge in a way that its right border, go to the right border of the screen?

Upvotes: 0

Views: 147

Answers (1)

backslash-f
backslash-f

Reputation: 8193

One way to implement what you described is to start with the TableView "outside" of the scene, with a negative Leading Space to the main view. Them when users press the button, the value of the Leading Space Constraint is set to zero.

Example:

enter image description here

Final result:

enter image description here

ViewController code:

class ViewController: UIViewController {

    @IBOutlet weak var tableViewLeadingMargin: NSLayoutConstraint!

    @IBAction func revealTableView(sender: UIButton) {
        UIView.animateWithDuration(
            3.0,
            animations: {
                self.tableViewLeadingMargin.constant = 0
                self.view.layoutIfNeeded()
            }
        )
    }
}

There's just one caveat: you'll need to adjust the width of the TableView programatically, based on the iPhone screen size.

The answer above the iPhone issue. For iPad, I'd suggest using Size Classes, creating a specific layout/constraints for them:

sizeClasses

To illustrate:

sizeClasseForIpad

Upvotes: 1

Related Questions