自定义导航栏(附带利用单例传值)
来源:互联网 发布:数据质量盘点标准 编辑:程序博客网 时间:2024/05/22 16:02
AppDelegate.m里面需要实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *VC = [ViewControllernew];
self.navtionViewControl = [[UINavigationControlleralloc]initWithRootViewController:VC];
self.navtionViewControl.navigationBar.hidden = YES;
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.windowsetRootViewController:self.navtionViewControl];
[self.windowmakeKeyAndVisible];
returnYES;
}
自定义导航栏.h文件里面需要实现
#import <UIKit/UIKit.h>
@protocol publicNavtionDelegate <NSObject>
@required
- (void)rightBtnSelect;
@optional
- (void)leftBtnSelect;
@end
@interface PublicNavtionBar : UIView
@property (nonatomic,assign)id<publicNavtionDelegate>navtioonBarDelegate;
//通用的初始化方法
-(id)initWithLeftBtn:(BOOL)leftBtnControl
withTitle:(NSString *)strTitle
withRightBtn:(BOOL)rightBtnControl
withRightBtnPicName:(NSString *)picName
withRightBtnSize:(CGSize)picSize;
@end
自定义导航栏.m文件里面需要实现
#import "PublicNavtionBar.h"
#import "AppDelegate.h"
#define MainView_Width [UIScreen mainScreen].bounds.size.width
#define IOS7_Y ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0?20:0)
#define kWide [UIScreen mainScreen].bounds.size.width
#define kPercenX kWide / 320
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define color_pink RGBCOLOR(223,79, 140)
@implementation PublicNavtionBar
@synthesize navtioonBarDelegate =_navtionBarDelegate;
- (id)initWithLeftBtn:(BOOL)leftBtnControl withTitle:(NSString *)strTitle withRightBtn:(BOOL)rightBtnControl withRightBtnPicName:(NSString *)picName withRightBtnSize:(CGSize)picSize{
self = [superinitWithFrame:CGRectMake(0,0,MainView_Width,44 +IOS7_Y)];
if (self)
{
//设置导航条背景颜色
[selfsetBackgroundColor:color_pink];
if (leftBtnControl) {
//返回按钮
UIButton * btnBack = [[UIButtonalloc]init];
[btnBack setTitleColor:[UIColorgreenColor] forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImageimageNamed:@"Public_btnBack.png"]forState:UIControlStateNormal];
[btnBack setFrame:CGRectMake(kPercenX,IOS7_Y, 44,44)];
[btnBack addTarget:selfaction:@selector(btnBack)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:btnBack];
}
UILabel * labelTitle = [[UILabelalloc]init];
[labelTitle setText:strTitle];
[labelTitle setFrame:CGRectMake(0,IOS7_Y, MainView_Width,44)];
[labelTitle setTextColor:[UIColorblackColor]];
[labelTitle setBackgroundColor:[UIColorclearColor]];
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[labelTitle setFont:[UIFontboldSystemFontOfSize:18]];
[selfaddSubview:labelTitle];
//右侧按钮
if (rightBtnControl) {
UIButton * btnRight = [[UIButtonalloc]init];
[btnRight setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];
[btnRight setBackgroundImage:[UIImageimageNamed:picName] forState:UIControlStateNormal];
[btnRight setFrame:CGRectMake(MainView_Width-picSize.width-10,IOS7_Y+(44-picSize.height)/2, picSize.width,picSize.height)];
[btnRight addTarget:selfaction:@selector(rightBtnSelelct)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:btnRight];
}
}
returnself;
}
//右侧按钮点击方法
-(void)rightBtnSelelct{
[self.navtioonBarDelegaterightBtnSelect];
}
-(void)btnBack{
if([self.navtioonBarDelegaterespondsToSelector:@selector(leftBtnSelect)]){
[self.navtioonBarDelegateleftBtnSelect];
}
else
{
AppDelegate * appdelegate = [UIApplicationsharedApplication].delegate;
[appdelegate.navtionViewControlpopViewControllerAnimated:YES];
}
}
@end
然后再创建一个单例(创建单例的方法)
.H文件里面需要实现
#import <Foundation/Foundation.h>
@interface Animal : NSObject
+(instancetype)shareTimeH;
@property (nonatomic,copy)NSString *name;
@end
.m里面需要实现的
#import "Animal.h"
@implementation Animal
+ (instancetype)shareTimeH{
staticAnimal *anima;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken,^{
anima = [[[selfclass]alloc]init];;
});
return anima;
}
seconderViewController.h 里面需要实现
#import "seconderViewController.h"
#import "PublicNavtionBar.h"
#import "ViewController.h"
#import "Animal.h"
@interface seconderViewController ()
{
UITextField *textField;
}
@end
@implementation seconderViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorredColor];
self.navigationController.navigationBar.hidden = YES;
PublicNavtionBar *bar = [[PublicNavtionBaralloc]initWithLeftBtn:YESwithTitle:@"返回"withRightBtn:YESwithRightBtnPicName:@"column_drag_normal.png"withRightBtnSize:CGSizeMake(44,44)];
[self.viewaddSubview:bar];
textField = [UITextFieldnew];
textField.backgroundColor = [UIColorgrayColor];
textField.frame =CGRectMake(100,100, 200,50);
[self.viewaddSubview:textField];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
Animal *anima = [AnimalshareTimeH];
anima.name =textField.text;
[self.navigationControllerpopViewControllerAnimated:YES];
}
ViewController.h 里面需要实现
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic,strong)UILabel *lable;
@end
ViewController.m 里面需要实现
#import "ViewController.h"
#import "PublicNavtionBar.h"
#import "seconderViewController.h"
#import "Animal.h"
@interface ViewController ()<publicNavtionDelegate>
{
Animal *anima;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgreenColor];
PublicNavtionBar *navBar = [[PublicNavtionBaralloc]initWithLeftBtn:NOwithTitle:@"王维传"withRightBtn:YESwithRightBtnPicName:@"column_drag_normal.png"withRightBtnSize:CGSizeMake(40,40)];
[navBar setNavtioonBarDelegate:self];
[self.viewaddSubview:navBar];
anima = [AnimalshareTimeH];
_lable = [UILabelnew];
_lable.backgroundColor = [UIColorredColor];
_lable.frame =CGRectMake(100,100, 200,50);
[self.viewaddSubview:_lable];
}
- (void)viewWillAppear:(BOOL)animated{
_lable.text =anima.name;
NSLog(@"--->%@",anima.name);
}
- (void)rightBtnSelect{
seconderViewController *secondVC = [seconderViewControllernew];
[self.navigationControllerpushViewController:secondVCanimated:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
seconderViewController *secondVC = [seconderViewControllernew];
[self.navigationControllerpushViewController:secondVCanimated:YES];
}
- 自定义导航栏(附带利用单例传值)
- 利用TabHost自定义底部导航栏
- 仿新浪头部导航栏(附带图片)
- 自定义navigation(导航栏)
- 自定义进度条控件(附带百分比的)
- iOS自定义导航栏-导航栏联动(一)
- iOS自定义导航栏-导航栏联动(二)
- 十四、自定义联系人导航(上)---字母导航栏
- UINavigationBar自定义导航栏
- 导航栏自定义按钮
- 自定义导航栏
- 自定义导航栏UINavigationBar
- 自定义导航栏
- 自定义导航栏按钮
- 自定义添加导航栏
- iOS自定义导航栏
- iOS自定义导航栏
- 导航栏自定义控件
- adb logcat 命令行用法
- oracle基本函数
- 展开拉伸动画---属性动画
- Android 操作SQLite基本用法
- CodeForces 166C
- 自定义导航栏(附带利用单例传值)
- Intent传递Map
- spring+mvc+mybatis+maven(上传+拦截+json出现下载问题)
- 11.Java 基础 - 泛型
- 习题3 数字和数学计算
- 面试出Android开发者的真正水平
- 11_Power数值的整数次方
- 仿淘宝的Flexible进行H5页面进行适配mobile
- Windows下C语言的Socket编程例子(TCP和UDP)