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];
    }
时间: 2024-10-11 04:13:20

ios 自定义导航栏和分割线的相关文章

IOS 自定义导航栏标题和返回按钮标题

IOS中自定义导航栏标题: UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)]; titleText.backgroundColor = [UIColor clearColor]; titleText.textColor=[UIColor whiteColor]; [titleText setFont:[UIFont systemFontOfSize:17.0]]; [titleTex

iOS 自定义导航栏笔记

一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC = [[TestViewController alloc] init]; UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.ro

ios 自定义导航栏,开启侧滑返回手势

自定义一个常用ListViewController .h文件 1 #import <UIKit/UIKit.h> 2 3 @interface ListViewController : UIViewController 4 5 -(void)diquButtonClick; 6 7 @end .m文件 1 // 2 // ListViewController.m 3 // OuLianWang 4 // 5 // Created by allenariel on 15/6/24. 6 // C

IOS 自定义导航栏

我们自己新建一个View,来自定义导航栏,如下代码: #import <UIKit/UIKit.h> @interface CustomNavigation : UIView typedef enum { customEventClickLBtn1 ,//点击了最左边的按钮 customEventClickRBtn1 , //最右边的按钮 customEventClickLBtn2 ,//点击了左边第二个按钮 customEventClickRBtn2 //点击了右边最靠左的按钮 }Custo

IOS 自定义导航栏背景

//- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; -(id)init { self=[super init]; if(self) { UINavigationBar *navBar=[self navigationBar]; if([navBar respondsTo

iOS 自定义导航栏的返回按钮

UIBarButtonItem * item  =    [UIBarButtonItem appearance]; UIImage* image = [UIImage imageNamed:@"back_icon"]; [item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlS

iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案

为相机制作闪光灯,在导航栏自定义了"闪光"图案,希望点击时变换图片,但是一直没有改变,原来是因为设置了Global Tint的颜色,所以系统会自动把图片的颜色改为Global Tint的颜色. 解决方案,设置图片时,添加:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal 源码: - (void) setFlashOn:(BOOL)isOn { if (self.captureDevice.hasFlash) { UIIm

一些关于iOS系统导航栏与自定义导航栏的事情

关于系统导航栏是真的让人又爱又恨,爱的是苹果本身对这个控件的封装已经是很完美了,包括内存.美化.渐变动画等等,一般来说,基本上所有需求都可以满足的.但是你要知道什么东西到了中国,就会发生翻天覆地的变化,例如后台的数据并发.在国内奇葩的产品设计之下,导航栏也是面目全非,反正我看了比较著名的APP,发现他们的导航栏基本都是自定义,其中牵扯最大的问题就是导航栏自身的隐藏.颜色渐变. 其实通过APP运行时,你可以看到系统NavigationBar的分层.一个navigationBar是分很多层的,并非我

实际iOS编程中遇到的自定义导航栏按钮,导致手势返回失效的解决方法

1\在实际编程过程中往往需要自定义导航栏上面的按钮,也就用: - (instancetype)initWithCustomView:(UIView *)customView; 但用了这个方法后可能会导致iOS7,8的手势返回失效,解决方法就是在自定义的导航栏的viewDidLoad方法中添加如下代码 注意:只有用系统的导航栏,或者继承于系统的导航栏才可以用Push方法,并且自带返回手势. - (void)viewDidLoad { [super viewDidLoad]; __weak type