Return to Video

06-06 Move View Get Out the Way

  • 0:00 - 0:03
    If you've been using the simulator
    keyboard to test our your version of
  • 0:03 - 0:07
    MemeMe, you may have
    noticed a little problem.
  • 0:07 - 0:09
    Especially when editing
    this bottom text field.
  • 0:11 - 0:12
    Wah-wah.
  • 0:13 - 0:17
    The keyboard covers up the text
    field and leaves the user stuck.
  • 0:17 - 0:21
    One can avoid this by shifting
    the entire view up, like so.
  • 0:23 - 0:26
    To isolate the keyboard issue,
    let's build a simple app.
  • 0:26 - 0:29
    Here, I've got an app with
    a text field and an image.
  • 0:29 - 0:30
    The text field is at the bottom, and
  • 0:30 - 0:33
    it gets covered up if we
    don't shift the view.
  • 0:33 - 0:35
    Shifting the view is simple.
  • 0:35 - 0:38
    Remember way back when we built
    Click Counter from scratch in Code?
  • 0:38 - 0:41
    To place our views,
    we use the frame property.
  • 0:41 - 0:43
    That's the property we'll use
    to shift the main view up.
  • 0:43 - 0:47
    More specifically, frame.origin.y.
  • 0:47 - 0:51
    The point where y equals zero
    is at the top of the screen.
  • 0:51 - 0:52
    To move the view up above the keyboard,
  • 0:52 - 0:55
    we subtract the height
    of the keyboard like so.
  • 0:56 - 0:59
    But how will we know when
    the keyboard is about to slide up?
  • 0:59 - 1:01
    This is where NS notifications come in.
  • 1:02 - 1:06
    In short, NS notifications provide a way
    to announce information throughout
  • 1:06 - 1:09
    a program, across classes.
  • 1:09 - 1:12
    To announce information like
    the keyboard appearing.
  • 1:12 - 1:13
    Or disappearing.
  • 1:13 - 1:18
    For more details, see the upcoming text
    note on the NS notification center.
  • 1:18 - 1:23
    Kind of like we subscribe to mailing
    lists, objects have to subscribe to or
  • 1:23 - 1:24
    observe notifications.
  • 1:25 - 1:26
    In this block of code,
  • 1:26 - 1:30
    the view controller is signing up to
    be notified when an event is coming up.
  • 1:30 - 1:33
    The event, UI keyboard will show.
  • 1:33 - 1:37
    We'll call this subscribe to keyboard
    notifications method up here and
  • 1:37 - 1:38
    view will appear.
  • 1:40 - 1:43
    When the event UI keyboard
    will show occurs,
  • 1:43 - 1:48
    the method keyboard will show is called
    and that method shifts the frame.
  • 1:48 - 1:50
    But right now, the compiler's
    complaining because we haven't
  • 1:50 - 1:53
    implemented this get
    keyboard height method.
  • 1:53 - 1:57
    Notifications carry information
    in the user info dictionary.
  • 1:57 - 2:02
    The UIKeyboardWillShowNotification
    carries the keyboard frame in its
  • 2:02 - 2:04
    user info dictionary.
  • 2:04 - 2:06
    So that's how we get
    the keyboard height.
  • 2:06 - 2:09
    So now lets see this
    keyboard shift in action.
  • 2:11 - 2:14
    But let's not forget,
    just like with mailing lists,
  • 2:14 - 2:16
    eventually we need to unsubscribe.
  • 2:16 - 2:19
    So here's the method to unsubscribe
    from keyboard notifications.
  • 2:20 - 2:24
    And we'll call this unsubscribe method
    up here and view will disappear.
Tytuł:
06-06 Move View Get Out the Way
Opis:

06-06 Move View Get Out the Way

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD788 - UIKit Fundamentals
Duration:
02:26

English subtitles

Revisions Compare revisions