Material Components (MDC) help developers implement Material Design. Created by a team of engineers and UX designers at Google, MDC features dozens of beautiful and functional UI components and is available for Android, iOS, web and Flutter.

What are Material Design and Material Components for iOS?

Material Design is a system for building bold and beautiful digital products. By uniting style, branding, interaction, and motion under a consistent set of principles and components, product teams can realize their greatest design potential.

Material Components for iOS (MDC iOS) unites design and engineering with a library of components for creating consistency across apps and websites. As the Material Design system evolves, these components are updated to ensure consistent pixel-perfect implementation and adherence to Google's front-end development standards. MDC is also available for the Android, web, and Flutter.

In this codelab, you'll build a login page using several MDC iOS components.

What you'll build

This codelab is the first of 4 codelabs that will guide you through building an app called Shrine, an e-commerce website that sells clothing and home goods. It will demonstrate how you can customize components to reflect any brand or style using MDC-iOS.

In this codelab, you'll build a login page for Shrine that contains:

MDC iOS components in this codelab

What you'll need

How would you rate your level of experience building iOS apps?

Novice Intermediate Proficient

Download the starter codelab app

Download starter app

The starter app is located within the material-components-ios-codelabs-master/MDC-101/ObjectiveC/Starter directory. Be sure to cd into that directory before beginning.

...or clone it from GitHub

To clone this codelab from GitHub, run the following commands:

git clone
cd material-components-ios-codelabs/MDC-101/ObjectiveC/Starter

Run the starter app

1. In Xcode open the workspace "Shrine.xcworkspace".

If you see the "Welcome to Xcode" window, click "Open another project...", navigate to the file and click open.

OR: Go to File > Open... and navigate to the file and click "Open".

OR: At the terminal enter:
open Shrine.xcworkspace

2. Run the app by pressing the play button at the top of the window.

Success! The starter code for Shrine's login screen should be running in your simulator. You should see the name "Shrine" and the Shrine logo just below it.

To begin, we'll add two text fields to our login page, where a user will be able to enter their username and password. We'll use the MDC Text Field component, which displays a floating label by default.

Add the required header

In LoginViewController.m, add the following header:

//TODO: Add MaterialTextFields #import
#import <MaterialComponents/MaterialTextFields.h>

Add for text field properties

In LoginViewController.m, add the following properties underneath the existing @property(nonatomic) UIImageView *logoImageView in @interface:

// Text Field Properties
//TODO: Add Text Field Properties
@property(nonatomic) MDCTextField *usernameTextField;
@property(nonatomic) MDCTextField *passwordTextField;

You'll also need a Text Input Controller for each text field.

@property(nonatomic) MDCTextInputControllerOutlined *usernameTextFieldController;
@property(nonatomic) MDCTextInputControllerOutlined *passwordTextFieldController;

Initialize the text fields and add them to the view

In LoginViewController.m, add the following code underneath the existing logoImageView initialization in the viewDidLoad: method.

// Text Field Init
//TODO: Instantiate Text Fields
self.usernameTextField = [[MDCTextField alloc] initWithFrame:CGRectZero];
  self.usernameTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.usernameTextField.delegate = self;
self.usernameTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
self.usernameTextField.backgroundColor = self.scrollView.backgroundColor;
  [self.scrollView addSubview:self.usernameTextField];

Now initialize and associate the Text Input Controller for Username Text Field.

self.usernameTextFieldController =
 alloc] initWithTextInput:self.usernameTextField];
self.usernameTextFieldController.placeholderText = @"Username";

Repeat the process for the password text field.

self.passwordTextField = [[MDCTextField alloc] initWithFrame:CGRectZero];
  self.passwordTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.passwordTextField.delegate = self;
self.passwordTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
self.passwordTextField.secureTextEntry = YES;
self.passwordTextField.backgroundColor = self.scrollView.backgroundColor;
[self.scrollView addSubview:self.passwordTextField];

self.passwordTextFieldController =
 alloc] initWithTextInput:self.passwordTextField];
  self.passwordTextFieldController.placeholderText = @"Password";

Position the text fields in the layout

To position the Text Fields, add this code to LoginViewController.m. underneath logoImageView's constraints:

  // Text Field Constraints
  //TODO: Add Text Field Constraints
  NSLayoutConstraint *usernameTopConstraint =
  [NSLayoutConstraint constraintWithItem:self.usernameTextField
  [constraints addObject:usernameTopConstraint];

  NSLayoutConstraint *centerUsernameConstraint =
  [NSLayoutConstraint constraintWithItem:self.usernameTextField
                              multiplier:1.f constant:0.f];
  [constraints addObject:centerUsernameConstraint];

  NSArray <NSLayoutConstraint *> *horizontalUsernameConstraints =
  [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[username]-|"
                                            views:@{ @"username" : self.usernameTextField }];
  [constraints addObjectsFromArray:horizontalUsernameConstraints];

  NSLayoutConstraint *passwordTopConstraint =
  [NSLayoutConstraint constraintWithItem:self.passwordTextField
  [constraints addObject:passwordTopConstraint];

  NSLayoutConstraint *centerPasswordConstraint =
  [NSLayoutConstraint constraintWithItem:self.passwordTextField
                              multiplier:1.f constant:0.f];
  [constraints addObject:centerPasswordConstraint];

  NSArray <NSLayoutConstraint *> *horizontalPasswordConstraints =
  [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[password]-|"
                                            views:@{ @"password" : self.passwordTextField }];
  [constraints addObjectsFromArray:horizontalPasswordConstraints];

Check for a minimum password length

Text fields can indicate if the field input is valid or if it contains an error.

You should:

Find the UITextFieldDelegate and add the following implementation of textFieldShouldReturn:

//TODO: Add Text Field Validation
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];

  // Text Field Validation
  if (textField == (UITextField *)self.passwordTextField &&
      self.passwordTextField.text.length < 8) {
    [self.passwordTextFieldController setErrorText:@"Password is too short" errorAccessibilityValue:nil];

  return NO;

Build and run

The interface should now show two text fields for Username and Password! Check out the floating label animation when you tap on a text field:

Next, we'll add two buttons to our login page: "Cancel" and "Next." We'll use the MDC Button component to finish building our interface.

Add the required header

In LoginViewController.m, add the following header:

//TODO: Add MaterialButtons #import
#import <MaterialComponents/MaterialButtons.h>

Create properties for the buttons

In LoginViewController.m, add the following properties to the @interface, underneath the text field properties created earlier:

// Button Properties
//TODO: Add Button Properties
@property(nonatomic) MDCFlatButton *cancelButton;
@property(nonatomic) MDCRaisedButton *nextButton;

Initialize the buttons and add them to the view

In LoginViewController.m, add the following code underneath the the text fields in the viewDidLoad: method:

  // Button Init
  //TODO: Instantiate Buttons
  self.cancelButton = [[MDCFlatButton alloc] init];
  self.cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
  [self.cancelButton setTitle:@"CANCEL" forState:UIControlStateNormal];
  [self.cancelButton addTarget:self
  [self.scrollView addSubview:self.cancelButton];

  self.nextButton = [[MDCRaisedButton alloc] initWithFrame:CGRectZero];
  self.nextButton.translatesAutoresizingMaskIntoConstraints = NO;
  [self.nextButton setTitle:@"NEXT" forState:UIControlStateNormal];
  [self.nextButton addTarget:self
  [self.scrollView addSubview:self.nextButton];

Add layout constraints to position the buttons

To position the Text Fields, add code to viewDidLoad: underneath the text field constraints:

  // Button Constraints
  //TODO: Add Button Constraints
  NSLayoutConstraint *cancelTopConstraint =
  [NSLayoutConstraint constraintWithItem:self.cancelButton
  [constraints addObject:cancelTopConstraint];

  NSLayoutConstraint *centerCancelConstraint =
  [NSLayoutConstraint constraintWithItem:self.cancelButton
                              multiplier:1.f constant:0.f];
  [constraints addObject:centerCancelConstraint];

  NSLayoutConstraint *centerButtonsConstraint =
  [NSLayoutConstraint constraintWithItem:self.cancelButton
                              multiplier:1.f constant:0.f];
  [constraints addObject:centerButtonsConstraint];

  NSArray <NSLayoutConstraint *> *horizontalButtonConstraints =
  [NSLayoutConstraint constraintsWithVisualFormat:@"H:[cancel]-[next]-|"
                                            views:@{ @"cancel" : self.cancelButton, @"next" : self.nextButton }];
  [constraints addObjectsFromArray:horizontalButtonConstraints];

  NSLayoutConstraint *scrollContentBottomConstraint =
                                            [NSLayoutConstraint constraintWithItem:self.nextButton
  [constraints addObject:scrollContentBottomConstraint];

Add button actions

Add the button action handlers.

#pragma mark - Action Handling

// Button Action Handlers
//TODO: Add Action Handlers
- (void)didTapNext:(id)sender {
  [self dismissViewControllerAnimated:YES completion:NULL];

- (void)didTapCancel:(id)sender {
  [self dismissViewControllerAnimated:YES completion:NULL];

Build and run

You should now see a page that contains these two buttons. Try it out!

Material Components for iOS has helped you create a beautiful login page that conforms to the Material Design guidelines, which looks and behaves consistently across all devices.

Next steps

Text Field and Button are two core components in MDC iOS, but there are many more! You can explore the rest of the components in MDC iOS.

Alternatively, head over to MDC-102: Material Design Structure and Layout to learn about AppBar and Card Collection. Thanks for trying Material Components. We hope you enjoyed this codelab!

I was able to complete this codelab with a reasonable amount of time and effort

Strongly agree Agree Neutral Disagree Strongly disagree

I would like to continue using Material Components in the future

Strongly agree Agree Neutral Disagree Strongly disagree