iOS UI Controls

iOS Segmented Control (UISegmentedControl)

  • Set up a Segmented Control to show different content of a view
    • Initialize a Segmented Control with different choices
    • Set up callback when choices are changed
      static NSArray *choices = nil;
      
      - (UISegmentedControl *)configTypeSegmentedControl
      {
        if (!choices)
           choices = [NSArray arrayWithObjects:@"First", @"Second", nil];
           UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:choices];
      
           segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
           [segmentedControl addTarget:self action:@selector(changeChoice:) forControlEvents:UIControlEventValueChanged];
           segmentedControl.selectedSegmentIndex = ...;  // Set the initial value
      
           return segmentedControl;
      }
      
      - (void)changeChoice:(UISegmentedControl *)segmentedControl
      {
        // Get the selected choice
        segmentedControl.selectedSegmentIndex
        ...
      }
      
      - (void)createUIChoices
      {
         self.navigationItem.titleView = [self configTypeSegmentedControl];
      }
      
  • Supported style for Segmented Control
    • UISegmentedControlStylePlain
    • UISegmentedControlStyleBordered
    • UISegmentedControlStyleBar

Use the Segmented Control as the middle view of the top navigation bar

   self.navigationItem.titleView = [self configTypeSegmentedControl];

UITextField

UITextField

  • When UITextField becomes the first responder, a keyboard shows up
    • Either by tapping on the field by a user
    • Or UITextField receives becomeFirstResponder
      - (void)viewWillAppear:(BOOL)animated
      {
        [super viewWillAppear:animated];
           ...
        [self.someTextField becomeFirstResponder];
      }
      
  • When a resignFirstResponder message is send to UITextField, the keyboard will be dismissed

Text can be extracted from the following delegate

  • when user hit return
    @interface MyViewController : UIViewController <UITextFieldDelegate>
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       if (textField.text.length) {
         [textField resignFirstResponder];
         ...
         return YES;
       } else {
         return NO;
       }
    }
    

    Or when a text field resign as first responder

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
       ...
    }
    

    When a text field resign as first responder

Control the appearance of the keyboard by setting the following properties

@property UITextAutocapitalizationType autocapitalizationType;
@property UITextAutocorrectionType autocorrectionType;
@property UIReturnKeyType returnKeyType;
@property BOOL secureTextEntry;
@property UIKeyboardType keyboardType
  • autocapitalizationType set up auto capitalize rule
  • returnKeyType set what is shown on the return key (Go, show ...)
  • secureTextEntry enable field like password to hide what a user type

The keyboard may obscure the text field, to re-adjust the view

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardAppeared:)
                                             name:UIKeyboardDidShowNotification
                                           object:self.view.window];
  • When the keyboard appears, notify the keyboardAppeared method

Other UITextField's property

@property BOOL clearsOnBeginEditing;
@property BOOL adjustsFontSizeToFitWidth;
@property CGFloat minimumFontSize;
@property NSString *placeholder;
@property UIImage *background;
@property UIImage *disabledBackground;