1

I have a UIImageView and a UITextView. I need the UITextView to expand over the UIImageView when the UITextView is tapped. I am not sure how to do this.

From this before expansion

To this after expansion

Taylor Simpson
  • 940
  • 1
  • 10
  • 27
  • Can you tell a bit more? What do you mean by expand over UIImageView? Do you want to make them have same points and size? – Ulaş Sancak May 11 '17 at 01:19
  • I added pictures for context – Taylor Simpson May 11 '17 at 01:23
  • 1
    Do you use Auto Layout? How the text view and image view attached to main view? Can you tell this with some details? You created and add subviews by programmatically or with storyboard or xib fles? – Ulaş Sancak May 11 '17 at 01:28
  • I usually use auto layout, but I haven't set anything up, because I wasn't sure what to do. I thought of many things, but they didn't work so I was asking what way others have done this. The image view and text view would be in a storyboard in a view controller. – Taylor Simpson May 11 '17 at 02:30
  • @Adrian Thank you so much :) – Taylor Simpson May 11 '17 at 03:57

3 Answers3

0

You can follow below steps:

  • In your view controller layout (Storyboard or codes), make sure you put UIImageView under UITextView.
  • In your IBAction where you handle UITextView tap event, just update its frame height to be equal with its controller height. (as seen on the screenshot).

    // one of several ways to update a view frame
    textView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

You can find other methods to update UITextView size here: How do I change UIView Size?

Community
  • 1
  • 1
Quang Nguyen
  • 2,600
  • 2
  • 17
  • 24
0

You can also do it with Constrains. All you need to change the constant value of specific constraint in the Action of UITapGesture on the UITextView.You can change the Constraints of Top of TextView and height of TextView.Try this!!

caldera.sac
  • 4,918
  • 7
  • 37
  • 69
Dupinder kaur
  • 188
  • 2
  • 12
0

So you're trying to update the height of a UIView with a tap gesture. You'll need the following IBOutlets:

  • An NSLayoutConstraint
  • A CGFloat variable to update it that you can reach from your view controller to alter your NSLayoutConstraint's constant
  • A UITapGestureRecognizer

Additionally, your UIImageView will need to be behind the other views. You can just drag it to the top of the View hierarchy in Interface Builder. The top view is at the back of the hierarchy:

enter image description here

Here goes:

Create IBOutlets for the view and the height of the UIView you want to animate:

@IBOutlet weak var bioView: UIView!
@IBOutlet weak var bioViewHeightConstraint: NSLayoutConstraint!

That is only a reference to the storyboard. If you want to alter bioViewHeightConstraint, you'll need to access its constant. So declare a variable for that:

var bioViewHeight: CGFloat! {
    /*
     Here we're using a property observer to execute code whenever
     this value is updated
     */

    didSet {
        print("stackViewHeight updated to \(bioViewHeight)")
        UIView.animate(withDuration: 0.5, delay: 0.0, options: [.curveEaseIn], animations: { 
            self.bioViewHeightConstraint.constant = self.bioViewHeight
            self.view.layoutIfNeeded()
        }) { _ in
            self.bioTextView.scrollRangeToVisible(NSRange(location: 0, length: 0))
            print("animation complete")
        }
    }
}

You'll see I put some animation code inside of this in didSet. So whenever you change bioViewHeight, bioViewHeightConstraint.constant will get updated.

Next, we configure a UITapGestureRecognizer with the following code:

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(toggleHeight(sender:)))
    tapGestureRecognizer.numberOfTapsRequired = 1
    bioView.addGestureRecognizer(tapGestureRecognizer)

The tap gesture code goes in viewDidLoad.

Next, we have to write the handler for your taps. Here's the code for that:

func toggleHeight(sender: UITapGestureRecognizer) {
    if bioViewHeightConstraint.constant == UIScreen.main.bounds.height / 2 {
        // true
        bioViewHeight = UIScreen.main.bounds.height - 30 // gives space for status bar
    } else {
        // false
        bioViewHeight = UIScreen.main.bounds.height / 2
    }
}

That's the basic code that can do what you're trying to do. Here's a link to a repo with the complete code.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • Thanks I'll take a look. I implemented something similar, but not nearly as nicely as you have. – Taylor Simpson May 11 '17 at 15:39
  • Thank you it works great! Do you know how I could add that blue line and make it follow as the view rises and lowers? – Taylor Simpson May 11 '17 at 19:05
  • I've never done anything w/ making non-rectangular views, but I think you'd want to google non-rectangular views in swift.I think the line at the top is just a border they colored blue. – Adrian May 11 '17 at 19:07
  • Alright thanks! I drew a line, but maybe making the view a different shape would work better. – Taylor Simpson May 11 '17 at 19:08