自己定义导航栏:

  1. // CustomNaviBarView.h
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5. @interface CustomNaviBarView : UIView
  6. {
  7. @private
  8. /**
  9. * 左側button
  10. */
  11. UIButton* _leftButton;
  12. /**
  13. * 右側button
  14. */
  15. UIButton* _rightButton;
  16. /**
  17. * 中部标签
  18. */
  19. UILabel* _navTitle;
  20. }
  21.  
  22. @property(nonatomic,strong)UIButton* leftButton;
  23. @property(nonatomic,strong)UIButton* rightButton;
  24. @property(nonatomic,strong)UILabel* navTitle;
  25.  
  26. /**
  27. * 返回一个自己定义导航条
  28. *
  29. * @param controller 控制器对象
  30. * @param leftTitle 导航左側文本,默认:@"取消"
  31. * @param rightTitle 导航右側文本
  32. * @param centerTitle 导航中部文本
  33. *
  34. * @return 导航条对象
  35. */
  36. - (CustomNaviBarView*)initCustomNaviBarViewOnController:(UIViewController*)controller leftTitle:(NSString*)leftTitle rightTitle:(NSString*)rightTitle centerTitle:(NSString*)centerTitle;
  37.  
  38. @end
  1. // CustomNaviBarView.m
  2.  
  3. #import "CustomNaviBarView.h"
  4. #import "Constant.h"
  5.  
  6. @implementation CustomNaviBarView
  7.  
  8. @synthesize leftButton = _leftButton;
  9. @synthesize rightButton = _rightButton;
  10. @synthesize navTitle = _navTitle;
  11.  
  12. - (CustomNaviBarView*)initCustomNaviBarViewOnController:(UIViewController*)controller leftTitle:(NSString*)leftTitle rightTitle:(NSString*)rightTitle centerTitle:(NSString*)centerTitle
  13. {
  14. //1.创建导航栏视图
  15. self = [super initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 65)];
  16. if (self != nil)//当导航视图没有载入成功的时候推出该方法
  17. {
  18. //1.为导航视图设置背景
  19. self.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:248 / 255.0 blue:248 / 255.0 alpha:1];
  20. [[controller navigationController] setNavigationBarHidden:YES animated:YES];
  21.  
  22. //2.导航面板左边的取消按钮
  23. _leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  24. if (_leftButton != nil)
  25. {
  26. _leftButton.frame = CGRectMake(15, 20, 65, 44);
  27. if (leftTitle != nil) {
  28. [_leftButton setTitle:leftTitle forState: UIControlStateNormal];
  29. }else
  30. {
  31. [_leftButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
  32. }
  33. [_leftButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
  34. _leftButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
  35. _leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  36. //[leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside)forControlEvents :UIControlEventTouchUpInside];
  37. [self addSubview:_leftButton];
  38. }
  39. //3.导航面板右边的公布按钮
  40. _rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  41. if (_rightButton != nil)
  42. {
  43. [_rightButton setFrame:CGRectMake(WIDTH_SCREEN - 80, 20, 65, 44)];
  44. if (_rightButton != nil) {
  45. [_rightButton setTitle:rightTitle forState: UIControlStateNormal];
  46. }else
  47. {
  48. //[rightButton setTitle:POST_CANCEL_BUTTON forState: UIControlStateNormal];
  49. }
  50. [_rightButton setTitleColor:[[UIColor alloc] initWithRed:0 green:158/255.0 blue:150/255.0 alpha:1.0]forState:UIControlStateNormal];
  51. _rightButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
  52. _rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  53. //[rightButton addTarget:self action:@selector(postButtonEventTouchUpInside)forControlEvents :UIControlEventTouchUpInside];
  54. [self addSubview:_rightButton];
  55. }
  56.  
  57. //4.导航面板中部文字
  58. _navTitle = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, WIDTH_SCREEN - 80 - 80, 44)];
  59. if (_navTitle != nil)
  60. {
  61. [_navTitle setTextColor:[UIColor blackColor]];
  62. if (centerTitle != nil)
  63. {
  64. _navTitle.text = centerTitle;
  65. }else
  66. {
  67. //navTitle.text = @"";
  68. }
  69. [_navTitle setTextAlignment:NSTextAlignmentCenter];
  70. _navTitle.font = [UIFont systemFontOfSize:18.0];
  71. [self addSubview:_navTitle];
  72. }
  73.  
  74. //5.在导航视图底加入切割线
  75. UIView *navDividingLine = [[UIView alloc] init];
  76. if (navDividingLine != nil)
  77. {
  78. navDividingLine.frame = CGRectMake(0, 20 + 44, WIDTH_SCREEN, 1);
  79. navDividingLine.backgroundColor = [UIColor colorWithRed:221 / 255.0 green:221 / 255.0 blue:221 / 255.0 alpha:1];
  80. [self addSubview:navDividingLine];
  81. }
  82.  
  83. //6.往view添加导航栏
  84. //[controller.view addSubview:navView];
  85. }
  86. return self;
  87. }
  88.  
  89. @end

怎样使用:

  1. //1.创建导航
  2. CustomNaviBarView* customNaviBarView = [[CustomNaviBarView alloc] initCustomNaviBarViewOnController:self leftTitle: nil rightTitle:NEWADDRESS_ADD_TITLE centerTitle:NEWADDRESS_NAVIGATION_TITLE];
  3. if (customNaviBarView != nil)
  4. {
  5. [customNaviBarView.leftButton addTarget:self action:@selector(cancelButtonEventTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  6. [customNaviBarView.rightButton addTarget:self action:@selector(addButtonEventTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  7. [self.view addSubview:customNaviBarView];
  8. }

自己定义切割线:

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface CustomDividingLine : UIView
  4.  
  5. /**
  6. * 创建一条切割线
  7. *
  8. * @param frame 位置及大小
  9. * @param color 背景色,假设为空默认:#f2f2f2
  10. *
  11. * @return 新创建的切割线
  12. */
  13. - (CustomDividingLine*)initDividingLineWithFrame:(CGRect)frame color:(UIColor*)color;
  14.  
  15. @end
  1. #import "CustomDividingLine.h"
  2.  
  3. @implementation CustomDividingLine
  4.  
  5. /**
  6. * 创建一条切割线
  7. *
  8. * @param frame 位置及大小
  9. * @param color 背景色
  10. *
  11. * @return 新创建的切割线
  12. */
  13. - (CustomDividingLine*)initDividingLineWithFrame:(CGRect)frame color:(UIColor*)color
  14. {
  15. //3.2.1切割线
  16. self = [super init];
  17. if (self != nil)
  18. {
  19. self.frame = frame;
  20. if (color != nil)
  21. {
  22. self.backgroundColor = color;
  23. }
  24. else
  25. {
  26. self.backgroundColor = [[UIColor alloc] initWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
  27. }
  28. }
  29. return self;
  30. }
  31.  
  32. @end

怎样使用:

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

ios 自己定义导航栏和切割线的更多相关文章

  1. iOS系统中导航栏的转场解决方案与最佳实践

    背景 目前,开源社区和业界内已经存在一些 iOS 导航栏转场的解决方案,但对于历史包袱沉重的美团 App 而言,这些解决方案并不完美.有的方案不能满足复杂的页面跳转场景,有的方案迁移成本较大,为此我们 ...

  2. 【转】iOS中设置导航栏标题的字体颜色和大小

    原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...

  3. iOS中设置导航栏标题的字体颜色和大小

    iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...

  4. iOS学习——更改导航栏的返回按钮的标题与颜色

    转载自:修改navigationController返回按钮颜色和文字 今天在做项目时遇到这个问题,试了很多方法都失败了.最后终于找到正确的方案了,在这里分享给大家. 引言 在iOS开发过程中,Nav ...

  5. iOS:自定义导航栏,随着tableView滚动显示和隐藏

    自定义导航栏,随着tableView滚动显示和隐藏 一.介绍 自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果.虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因 ...

  6. 解决ios7.0 以后自己定义导航栏左边button靠右的问题

    1.自己定义button //左button UIButton *leftBtn = [[UIButton , , , )]; [leftBtn addTarget:self action:@sele ...

  7. iOS 动态修改导航栏颜色 UINavigationBar

    示例 所谓动态修改  意思是 在当前页面滚动的过程中 亦或 是在 触发返回事件\进入一个新的页面  导航栏的动态变化 由于系统级别的navBar 高度集成  很多自己想实现的功能 很不好弄 如果是通过 ...

  8. iOS 系统根据导航栏和状态栏自动修改布局

    问题 条件:1.有一个全屏大小的带导航的controller 2.隐藏导航栏,最顶上还会留出状态栏的位置,而不是全屏显示 解决方法 self.automaticallyAdjustsScrollVie ...

  9. iOS 滑动隐藏导航栏-三种方式

    /** 1隐藏导航栏-简单- */    self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...

随机推荐

  1. 学习selenium所须要具备的技术

    学习selenium所须要具备的知识或技术 1.selenium进行的自己主动化測试是基于ui层面的,所以html,css,javascript基本上是不可缺少的,至于javascript,有非常多的 ...

  2. 开始学javascript基础

    JavaScript非常值得我们学习. 1)所有主浏览器都支持JavaScript. 2) 目前,全世界大部分网页都使用JavaScript. 3) 它可以使网页呈现各种动态效果. 4)作为一个Web ...

  3. 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”

    最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...

  4. JS文件中加载jquery.js

    原文链接:http://blog.csdn.net/whatday/article/details/39553451 最近有一个需求: 1.在一个html中只能引入一个JS文件 不能有JS代码和其他J ...

  5. Lucene文件扩展名

    名称 文件后缀 描述 段文件(Segments File) segments.gen segments_N 存储提交点信息 锁文件(Lock File) write.lock 用来阻止多个indexW ...

  6. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  7. mysql 日期时间运算函数(转)

      DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03'); ...

  8. jquery uploadify插件多文件上传

    1.jquery uploadify 下载:http://www.uploadify.com/ 2.安装:解压后拷贝的工程目录下面,如:WebRoot/uploaddify 3.配置项说明: uplo ...

  9. Python一路走来 - 模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  10. Halloween party

    https://www.hackerrank.com/challenges/halloween-party def main(): t = int(raw_input()) for _ in rang ...