Imitation Sina Weibo iOS client (v5.2.8)--implementation of drop-down menu bar

Source: Internet
Author: User
Tags uikit

Reprint please indicate source: http://blog.csdn.net/android_ls/article/details/45877983

Disclaimer: Sina Weibo project, the use of all the image resources are derived from the official Sina Weibo iOS client, the purpose of writing this application is to learn communication, such as infringement please inform, I will promptly replace the relevant pictures used.


Then the previous blog post, this article we talk about Sina Weibo navigation bar, click on the middle section of the title (Titleview) Pop-up drop-down menu is how to achieve.

1, custom navigation bar in the middle of the Titleview, the code is as follows:

  Set the Titleview in the middle of the navigation bar    _titlebutton = [self titleviewwithnickname:@ "the dream between the fingers"];    Self.navigationItem.titleView = _titlebutton;

2, in the UIButton default has ImageView and Titlelabel two sub-view, the default ImageView is on the left, and Titlelabel is on the right. We see on the first page of Sina Weibo, Titleview shows Titlelabel on the left, ImageView on the right side of Titleview, we try to adjust the display position of the UIButton neutron view, the specific implementation code is as follows:

#pragma mark set the titleview-(UIButton *) in the middle of the navigation bar Titleviewwithnickname: (NSString *) nickname{UIButton *titlebutton = [[Uibut]    Ton alloc] init];    Set up pictures and text [Titlebutton settitle:nickname forstate:uicontrolstatenormal];    [Titlebutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    TitleButton.titleLabel.font = [Uifont systemfontofsize:18];    [Titlebutton setimage:[uiimage imagenamed:@ "Navigationbar_arrow_down"] forstate:uicontrolstatenormal];    [Titlebutton setimage:[uiimage imagenamed:@ "navigationbar_arrow_up"] forstate:uicontrolstateselected];    90 40 These two values are now casually written titlebutton.imageedgeinsets = Uiedgeinsetsmake (0, 90, 0, 0);    Titlebutton.titleedgeinsets = uiedgeinsetsmake (0, 0, 0, 40); 130 This value is now casually written, the following will be changed to automatically calculate the length according to the content titlebutton.size = Cgsizemake (+);//Titlebutton.backgroundcolor = [Uicolor re        Dcolor];        [Titlebutton addtarget:self Action: @selector (Titleclick:) forcontrolevents:uicontroleventtouchupinside]; Return Titlebutton;} 

Note: There is no need to be detained in this way, other ways can be implemented (such as customizing a view to add two sub-view to it).

3, the following adjustments:



4, click the custom Titleview, pop-up drop-down menu, the implementation code is as follows:

#pragma mark Click on the title event handler on the navigation Bar-(void) Titleclick: (UIButton *) titlebutton{    //1. Create a drop-down menu    Dropdownmenuview * Dropdownmenuview = [[Dropdownmenuview alloc] init];    Set drop-down menu popup, destroy event listener    dropdownmenuview.delegate = self;        2. Set the content to be displayed    titlemenuviewcontroller *TITLEMENUVC = [[Titlemenuviewcontroller alloc] init];    Titlemenuvc.dropdownmenuview = Dropdownmenuview;    Titlemenuvc.delegate = self;        TitleMenuVC.view.width = KMOBILEPHONESCREENWIDTH/2;    TitleMenuVC.view.height = KMOBILEPHONESCREENHEIGHT/2;    Dropdownmenuview.contentcontroller = TITLEMENUVC;        3. Display the drop-down menu    [Dropdownmenuview Showfrom:titlebutton];}

5, let the current controller implement Dropdownmenudelegate and Titlemenudelegate protocol, implement the corresponding function, the specific code is as follows:

@interface Homeviewcontroller () <dropdownmenudelegate, titlemenudelegate>{    UIButton *_titlebutton;} @end

#pragma mark-dropdownmenudelegate#pragma mark drop-down menu was destroyed-(void) Dropdownmenudiddismiss: (Dropdownmenuview *) menu{    Let the indicator arrow down    UIButton *titlebutton = (UIButton *) Self.navigationItem.titleView;    titlebutton.selected = NO;} #pragma mark drop-down menu shows-(void) Dropdownmenudidshow: (Dropdownmenuview *) menu{    //Let the indicator arrows up    UIButton *titlebutton = ( UIButton *) Self.navigationItem.titleView;    titlebutton.selected = YES;} #pragma mark-titlemenudelegate-(void) Selectatindexpath: (Nsindexpath *) Indexpath title: (NSString *) title{    MyLog (@ "Indexpath =%ld", indexpath.row);    MyLog (@ "Currently selected%@", title);        Modify the title of the navigation bar    [_titlebutton settitle:title forstate:uicontrolstatenormal];        Call to return the corresponding microblog data according to the search criteria    //...}

6, first to see what has been achieved, there is a picture of the truth.



Click anywhere in the screen except the drop-down menu, or click an item in the drop-down menu (for example, click on "Circle of Friends") to have the drop-down menu destroyed.



Click Titleview again, such as:



7, the drop-down menu implementation Core Class (DROPDOWNMENUVIEW.H) The source code is as follows:

  dropdownmenuview.h//  sinaweibo////  Created by Android_ls on 15/5/20.//  Copyright (c) 2015 Android_ls. All rights reserved.////drop-down menu components #import <UIKit/UIKit.h> @class dropdownmenuview; @protocol dropdownmenudelegate <NSObject> @optional-(void) Dropdownmenudiddismiss: (Dropdownmenuview *) menu;-(void) Dropdownmenudidshow: ( Dropdownmenuview *) menu; @end @interface dropdownmenuview:uiview@property (nonatomic, weak) id< Dropdownmenudelegate> delegate; #pragma mark displays the menu below the specified UIView-(void) Showfrom: (UIView *) from; #pragma mark destroys the drop-down menu-( void) dismiss;//to display the content controller @property (nonatomic, strong) Uiviewcontroller *contentcontroller; @end

the drop-down menu implements the core class (DROPDOWNMENUVIEW.M) source code as follows:

dropdownmenuview.m//sinaweibo////Created by Android_ls on 15/5/20.//Copyright (c) 2015 Android_ls. All rights reserved.//#import "DropdownMenuView.h" @interface Dropdownmenuview () {//container to display specific contents Uiimageview * _conta Inerview;}    @end @implementation dropdownmenuview-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame];                if (self) {//clears the default background color Self.backgroundcolor = [Uicolor Clearcolor];        Add a gray image as the background of the drop-down menu _containerview = [[Uiimageview alloc] init];        _containerview.image = [UIImage imagenamed:@ "Popover_background"];        _containerview.userinteractionenabled = YES;    [Self addsubview:_containerview]; } return self;}        -(void) Setcontentcontroller: (Uiviewcontroller *) contentcontroller{_contentcontroller = Contentcontroller;    UIView * content = Contentcontroller.view;    Adjust the position of the content content.x = 7;        Content.y = 13;    _containerview.height = Cgrectgetmaxy (content.frame) + 9; _cOntainerview.width = Cgrectgetmaxx (content.frame) + 7; Add content to the gray image [_containerview addsubview:content];} #pragma mark displays the menu below the specified UIView-(void) Showfrom: (UIView *) from{//1. Get the top window UIWindow *window = [[UIApplication Shared        Application].windows Lastobject];        2. Add yourself to the window [Windows Addsubview:self];        3. Set the size self.frame = window.bounds; 4. Adjust the position of the gray image//By default, frame is the coordinate origin in the upper-left corner of the parent control//conversion coordinate system cgrect Newframe = [from ConvertRect:from.bounds Toview:window    ];    _containerview.centerx = Cgrectgetmidx (newframe);        _containerview.y = Cgrectgetmaxy (newframe); Notifies the outside world that it shows if ([Self.delegate respondstoselector: @selector (dropdownmenudidshow:)]) {[Self.delegate dropdownm    Enudidshow:self];        }} #pragma mark destroys the drop-down menu-(void) dismiss{[self removefromsuperview]; Notify the outside world that he has been destroyed if ([Self.delegate respondstoselector: @selector (Dropdownmenudiddismiss:)]) {[Self.delegate dropd    Ownmenudiddismiss:self]; }} #pragma mark clicked on his own execution pinDestruction action-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[self Dismiss];} @end

8, TitleMenuViewController.h the source code as follows:

  titlemenuviewcontroller.h//  sinaweibo////  Created by Android_ls on 15/5/20.//  Copyright (c) 2015 Android_ls. All rights reserved.//#import <UIKit/UIKit.h> @class dropdownmenuview @protocol titlemenudelegate <nsobject > #pragma mark is currently selected which line @required-(void) Selectatindexpath: (Nsindexpath *) Indexpath title: (nsstring*) title;@ End@interface Titlemenuviewcontroller:uitableviewcontroller@property (nonatomic, weak) id<TitleMenuDelegate> Delegate, @property (nonatomic, weak) Dropdownmenuview * Dropdownmenuview; @end

The source code of TITLEMENUVIEWCONTROLLER.M is as follows:

titlemenuviewcontroller.m//sinaweibo////Created by Android_ls on 15/5/20.//Copyright (c) 2015 Android_ls. All rights reserved.//#import "TitleMenuViewController.h" #import "DropdownMenuView.h" @interface Titlemenuviewcontroller () @property (nonatomic, strong) Nsmutablearray * data; @end @implementation        titlemenuviewcontroller-(void) viewdidload {[Super viewdidload];    _data = [Nsmutablearray array];    [_data addobject:@ "Home"];    [_data addobject:@ "friend Circle"];    [_data addobject:@ "group Weibo"];    [_data addobject:@ "my Weibo"];    [_data addobject:@ "special attention"];    [_data addobject:@ "Celebrity Star"];    [_data addobject:@ "colleague"];    [_data addobject:@ "classmate"]; [Self.tableview Reloaddata];} #pragma mark-table View data source-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return _data.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static    NSString *id = @ "Statuscell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];    if (!cell) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];    } NSString * name = _data[indexpath.row];    Cell.textLabel.text = name; return cell;}    #pragma mark Cell Click event handler-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    [TableView Deselectrowatindexpath:indexpath Animated:yes];    if (_dropdownmenuview) {[_dropdownmenuview dismiss];    } if (_delegate) {[_delegate selectatindexpath:indexpath Title:_data[indexpath.row]]; }} @end


Come here tonight, some tired, I go to sleep first, good night.


Imitation Sina Weibo iOS client (v5.2.8)--implementation of drop-down menu bar

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.