Mike Z
Mike Z

Reputation: 4111

Making a view appear like twitter app does with new tweet (iPad)

I'm trying to achieve an effect similar to what twitter does when you bring up the new tweet dialogue. They drop down a view from the top, shrinking the other views but still allowing you to interact with all of them if you dismiss the keyboard. It obviously isn't a modal view, but I can't put my finger on what the starting point to do something similar to this would be.

View appears

Interacting with the other views still

Upvotes: 2

Views: 332

Answers (1)

danh
danh

Reputation: 62686

It looks straight-forward as a view hierarchy, just cleverly dressed with art. The bottom is the regular interface, above is a view containing the UITextView with some nice notepad art around it.

One way to achieve this is to hang two subviews under the view controller's main view. The first child contains the notepad art and the text view. It's positioned at 0,-NOTEPAD_HEIGHT. The second child is at 0,0 and occupies the entire parent view's bounds.

The compose button tells the text view to become first responder, and when editing begins...

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [self setNotepadHidden:NO animated:YES];
}

I often make a show/hide method of the following form to rearrange things like this ...

- (void)setNotepadHidden:(BOOL)hidden animated:(BOOL)animated {

    NSTimeInterval duration = (animated)? 0.3 : 0.0;
    CGFloat offset = (hidden)? -NOTEPAD_HEIGHT : NOTEPAD_HEIGHT;

    [UIView animateWithDuration:duration animations:^{
        self.firstChild.frame = CGRectOffset(self.firstChild.frame, 0.0, offset);
        self.secondChild.frame = UIEdgeInsetsInsetRect(self.secondChild.frame, UIEdgeInsetsMake(offset, 0, 0, 0));
    }];
}

Call with ...Hidden:YES whenever you want to hide it again. Make sure that the second child's subviews have autoresizing behavior setup so that they do the right thing when their parent shrinks.

I often find the need for one like this, also...

- (BOOL)isNotepadHidden {
    return self.firstChild.frame.origin.y < 0.0;
}

Hopefully, that's a good start.

Upvotes: 1

Related Questions