iOS custom navigation bar and split line

Source: Internet
Author: User

Custom navigation bar:

  customnavibarview.h#import <UIKit/UIKit.h> @interface customnavibarview:uiview{@private    /**     *  left button */    uibutton* _leftbutton;    /**     *  right button */    uibutton* _rightbutton;    /**     *  Middle Label */    uilabel* _navtitle;} @property (Nonatomic,strong) uibutton* LeftButton; @property (Nonatomic,strong) uibutton* Rightbutton; @property ( Nonatomic,strong) uilabel* navtitle;/** *  returns a custom navigation bar * *  @param  Controller Controllers Object *  @param Lefttitle   navigation left text, default: @ "Cancel" *  @param righttitle  navigation right text *  @param centertitle Navigation Middle Text * *  @return Navigation bar Object */-(customnavibarview*) Initcustomnavibarviewoncontroller: (uiviewcontroller*) controller lefttitle: (NSString *) Lefttitle Righttitle: (nsstring*) righttitle centertitle: (nsstring*) centertitle; @end

Customnavibarview.m#import "CustomNaviBarView.h" #import "Constant.h" @implementation Customnavibarview@synthesize LeftButton = _leftbutton; @synthesize Rightbutton = _rightbutton; @synthesize Navtitle = _ navtitle;-(customnavibarview*) Initcustomnavibarviewoncontroller: (uiviewcontroller*) controller leftTitle: ( nsstring*) Lefttitle Righttitle: (nsstring*) righttitle centertitle: (nsstring*) centertitle{//1. To create a navigation bar view self = [Super I    Nitwithframe:cgrectmake (0, 0, Width_screen, 65)]; if (self! = nil)//When the Navigation view is not loaded successfully, the method is introduced {//1. Setting the background for the navigation view Self.backgroundcolor = [Uicolor colorwithred:248/        255.0 green:248/255.0 blue:248/255.0 alpha:1];                [[Controller Navigationcontroller] Setnavigationbarhidden:yes Animated:yes];        2. The Cancel button on the left side of the navigation panel _leftbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];            if (_leftbutton! = nil) {_leftbutton.frame = CGRectMake (15, 20, 65, 44); if (lefttitle! = nil) {[_lefTButton Settitle:lefttitle Forstate:uicontrolstatenormal];            }else {[_leftbutton Settitle:post_cancel_button forstate:uicontrolstatenormal]; } [_leftbutton Settitlecolor:[[uicolor alloc] initwithred:0 green:158/255.0 blue:150/255.0 Alpha:1.0]forState:U            Icontrolstatenormal];            _leftbutton.titlelabel.font = [Uifont systemfontofsize:16.0];            _leftbutton.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentleft; [LeftButton addtarget:self Action: @selector (cancelbuttoneventtouchupinside) forControlEvents:            UIControlEventTouchUpInside];        [Self Addsubview:_leftbutton];        //3. The Publish button to the right of the navigation panel _rightbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];            if (_rightbutton! = nil) {[_rightbutton setframe:cgrectmake (width_screen-80, 20, 65, 44)]; if (_rightbutton! = nil) {[_rightbutton settitle:righttitle Forstate:uicontrolstatenormal];            }else {//[rightbutton Settitle:post_cancel_button forstate:uicontrolstatenormal]; } [_rightbutton Settitlecolor:[[uicolor alloc] initwithred:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:            UIControlStateNormal];            _rightbutton.titlelabel.font = [Uifont systemfontofsize:16.0];            _rightbutton.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentright; [Rightbutton addtarget:self Action: @selector (postbuttoneventtouchupinside) forControlEvents:            UIControlEventTouchUpInside];        [Self Addsubview:_rightbutton];  }//4. The middle text of the navigation panel _navtitle = [[UILabel alloc] Initwithframe:cgrectmake (+, width_screen-80-80,        44)];            if (_navtitle! = nil) {[_navtitle settextcolor:[uicolor blackcolor]];      if (centertitle! = nil) {_navtitle.text = Centertitle;      }else {//navtitle.text = @ "";            } [_navtitle Settextalignment:nstextalignmentcenter];            _navtitle.font = [Uifont systemfontofsize:18.0];        [Self addsubview:_navtitle];        }//5. Add a split line at the bottom of the Navigation view UIView *navdividingline = [[UIView alloc] init];            if (navdividingline! = nil) {navdividingline.frame = CGRectMake (0, + width_screen, 1);            Navdividingline.backgroundcolor = [Uicolor colorwithred:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];        [Self addsubview:navdividingline];    }//6. Add navigation bar to view//[controller.view Addsubview:navview]; } return self;} @end

How to use:

    1. Create navigation    customnavibarview* Customnavibarview = [[Customnavibarview alloc] Initcustomnavibarviewoncontroller: Self Lefttitle:nil righttitle:newaddress_add_title centertitle:newaddress_navigation_title];    if (Customnavibarview! = nil)    {        [Customnavibarview.leftbutton addtarget:self action: @selector ( cancelbuttoneventtouchupinside) forcontrolevents:uicontroleventtouchupinside];        [Customnavibarview.rightbutton addtarget:self Action: @selector (addbuttoneventtouchupinside) forControlEvents: UIControlEventTouchUpInside];        [Self.view Addsubview:customnavibarview];    }


Custom Split lines:

#import <UIKit/UIKit.h> @interface customdividingline:uiview/** *  Create a split line * *  @param frame position and size *  @ param color Background color, if blank default: #f2f2f2 * *  @return newly created split line */-(customdividingline*) Initdividinglinewithframe: (CGRect) Frame color: (uicolor*) color; @end

#import "CustomDividingLine.h" @implementation customdividingline/** *  Create a split line * *  @param frame position and size *  @ param Color Background * *  @return newly created split line */-(customdividingline*) Initdividinglinewithframe: (CGRect) frame color: ( uicolor*) color{    //3.2.1 split line self    = [super init];    if (self! = nil)    {        self.frame = frame;        if (color = nil)        {            self.backgroundcolor = color;        }        else        {            Self.backgroundcolor = [[Uicolor alloc] initwithred:242/255.0 green:242/255.0 blue:242/255.0 Alpha : 1.0];        }    }    return self;} @end

How to use:

    3. Split Line    customdividingline* customdividingline = [[Customdividingline alloc]initdividinglinewithframe: CGRectMake (0, + +, Width_screen, 1) color:nil];    if (customdividingline! = nil)    {        [Self.view addsubview:customdividingline];    }



iOS custom navigation bar and split line

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.