ios 自定义导航栏和分割线

自定义导航栏:

//  CustomNaviBarView.h

#import <UIKit/UIKit.h>

@interface CustomNaviBarView : UIView
{
@private
    /**
     *  左侧按钮
     */
    UIButton* _leftButton;
    /**
     *  右侧按钮
     */
    UIButton* _rightButton;
    /**
     *  中部标签
     */
    UILabel* _navTitle;
}

@property(nonatomic,strong)UIButton* leftButton;
@property(nonatomic,strong)UIButton* rightButton;
@property(nonatomic,strong)UILabel* navTitle;

/**
 *  返回一个自定义导航条
 *
 *  @param controller  控制器对象
 *  @param leftTitle   导航左侧文本,默认:@"取消"
 *  @param rightTitle  导航右侧文本
 *  @param centerTitle 导航中部文本
 *
 *  @return 导航条对象
 */
- (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.创建导航栏视图
    self = [super initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 65)];
    if (self != nil)//当导航视图没有加载成功的时候推出该方法
    {
        //1.为导航视图设置背景
        self.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1];
        [[controller navigationController] setNavigationBarHidden:YES animated:YES];
        
        //2.导航面板左边的取消按钮
        _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:UIControlStateNormal];
            _leftButton.titleLabel.font            = [UIFont systemFontOfSize: 16.0];
            _leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            //[leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)forControlEvents :UIControlEventTouchUpInside];
            [self addSubview:_leftButton];
        }
        //3.导航面板右边的发布按钮
        _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.导航面板中部文字
        _navTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 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.在导航视图底添加分割线
        UIView *navDividingLine = [[UIView alloc] init];
        if (navDividingLine != nil)
        {
            navDividingLine.frame = CGRectMake(0, 20 + 44, WIDTH_SCREEN, 1);
            navDividingLine.backgroundColor = [UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1];
            [self addSubview:navDividingLine];
        }
        
        //6.往view增加导航栏
        //[controller.view addSubview:navView];
    }
    return self;
}

@end

如何使用:

    //1.创建导航
    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];
    }


自定义分割线:

#import <UIKit/UIKit.h>

@interface CustomDividingLine : UIView

/**
 *  创建一条分割线
 *
 *  @param frame 位置及大小
 *  @param color 背景色,如果为空默认:#f2f2f2
 *
 *  @return 新创建的分割线
 */
- (CustomDividingLine*)initDividingLineWithFrame:(CGRect)frame color:(UIColor*)color;

@end

#import "CustomDividingLine.h"

@implementation CustomDividingLine

/**
 *  创建一条分割线
 *
 *  @param frame 位置及大小
 *  @param color 背景色
 *
 *  @return 新创建的分割线
 */
- (CustomDividingLine*)initDividingLineWithFrame:(CGRect)frame color:(UIColor*)color
{
    //3.2.1分割线
    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

如何使用:

    //3.分割线
    CustomDividingLine* customDividingLine = [[CustomDividingLine alloc]initDividingLineWithFrame:CGRectMake(0, 65 + 100, WIDTH_SCREEN, 1) color:nil];
    if (customDividingLine != nil)
    {
        [self.view addSubview:customDividingLine];
    }



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。