IOS development encapsulates a horizontal menu bar class tutorial, ios menu bar

Source: Internet
Author: User

IOS development encapsulates a horizontal menu bar class tutorial, ios menu bar

To develop a relatively large software, you need to create a menu bar that allows you to quickly query and select what you need. A common menu bar is composed of a series of buttons and images. You need to manually add buttons and images for each menu item during creation. If there are multiple menu items, choose create loop. The current experience is to encapsulate a list menu bar class in the horizontal direction. After the class is encapsulated, you can save the complicated process of re-creation and layout every time you use it. Greatly improve development efficiency.

Create a project and View Controller

1. Create an empty project and create a UIViewController;

2. Select a project, right-click-New File... Select "Cocoa Touch Class"-Next, give a reasonable name to ViewController, and then click Finish;

3. In the AppDelegate. m file, include # import "ViewController. h ";

4. initialize and create the ViewController and include it with the navigation bar controller. Set it as the Root View Controller.

Create an encapsulation horizontal menu bar class

1. Select a project, right-click-New File... Select "Cocoa Touch Class"-Next;

2. Select a view inherited from UIView and name it HorizontalMenuView;

3. After Next, select the storage path (usually by default), and Create is complete.

Create attributes and methods for HorizontalMenuView

1. Open the HorizontalMenuView. h file;

2. Create a menu name array and set the menu name method setNameWithArray.

Write the setNameWithArray Implementation Method

1. Before Xcode6.0, the m file of UIView will have the initWithFrame method, but Xcode6.0 will not. Therefore, you can consider writing the code in the drawRect method;

2. Now the menu name setting method is created independently, so the Code such as the Add button is transferred to the setNameWithArray implementation method;

3. Assign the value to the global variable _ menuArray of the menu name.

Display controls for HorizontalMenuView

1. Create a button for _ menuArray based on the number of _ menuArray elements;

2. Set the button identifier tag;

3. Set the font size and color status of the button;

4. Click the Add button to respond to the event;

5. Add the split line, bottom line, and selected underline.

Implement button click RESPONSE event

1. Retrieve all sub-classes of self through traversal;

2. determine whether it is a button class [UIButton class];

3. For UIButton, enable the buttons Based on the clicked sender. tag;

4. Move the underline Based on the clicked sender. tag animation.

Add a protocol proxy to implement value transfer

1. Add the protocol in HorizontalMenuView. h;

2. Obtain the selected menu by writing the Protocol method in the Protocol;

3. Create a protocol delegate;

4. Click the implementation method in HorizontalMenuView. m to obtain the base value.

Usage

1. In ViewController. m, include # import "HorizontalMenuView. h ";

2. Add a protocol delegate;

3. Create and initialize HorizontalMenuView and add it to the view;

4. Set the menu name array;

5. Set the delegate agent;

6. implement the Protocol.

Code Running Effect

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.