iOS 自定义状态栏和导航栏详细介绍
程序员文章站
2024-02-14 08:08:23
ios 自定义状态栏和导航栏
开发ios app...
ios 自定义状态栏和导航栏
开发ios app 经常会根据需求更改状态栏和导航栏,这里整理了几种方法,大家可以看下。
导航栏透明
-(void)viewwillappear:(bool)animated { //viewwillappear中设置透明 [super viewwillappear:animated]; [self.navigationbar setbackgroundimage:[uiimage new] forbarmetrics:uibarmetricsdefault]; //用空图片填充机位透明 [self.navigationbar setshadowimage:[uiimage new]];//navibar底部的seperatorline } -(void)viewdiddisappear:(bool)animated { //viewwillappear中设置恢复 [super viewdiddisappear:animated]; [self.navigationbar setbackgroundimage:nil forbarmetrics:uibarmetricsdefault]; [self.navigationbar setshadowimage:shadowimage]; }
导航栏渐变
barimageview = self.navigationcontroller.navigationbar.subviews.firstobject; -(void)scrollviewdidscroll:(uiscrollview *)scrollview { cgfloat minalphaoffset = - 64; cgfloat maxalphaoffset = 200; cgfloat offset = scrollview.contentoffset.y; cgfloat alpha = (offset - minalphaoffset) / (maxalphaoffset - minalphaoffset); _barimageview.alpha = alpha; }
状态栏字体颜色改变
[uiapplication sharedapplication].statusbarstyle = uistatusbarstyledefault;//黑色 [uiapplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent;//白色
导航栏隐藏
如果导航栏自定义度高,需要完全自己重写,可以隐藏原来的导航栏,并定义一个新的view
-(void)viewwillappear:(bool)animated { [super viewwillappear:animated]; self.navigationcontroller.navigationbarhidden = yes; } -(void)viewdiddisappear:(bool)animated { [super viewdiddisappear:animated]; self.navigationcontroller.navigationbarhidden = no; } -(void)ys_initnavigationbar { self.navigationcontroller.interactivepopgesturerecognizer.delegate = (id)self; // 保留右滑pop的手势 _navibar = [[uiview alloc] initwithframe:cgrectmake(0, 0, self.view.width, 64)]; _navibar.backgroundcolor = [uicolor whitecolor]; [self.view addsubview:_navibar]; uiview *line = [[uiview alloc] initwithframe:cgrectmake(0, _navibar.height-0.5, self.view.width, 0.5)]; line.backgroundcolor = [uicolor colorforhex:@"f0f0f0"]; [_navibar addsubview:line]; // 返回 backbutton = [uibutton buttonwithtype:uibuttontypecustom]; backbutton.frame = cgrectmake(0, 20, 44, 44); [backbutton addtarget:self action:@selector(goback:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:backbutton]; // 标题 navilable = [[uibutton alloc] initwithframe:cgrectmake(44, 20, self.view.width-44*2, 44)]; navilable.backgroundcolor = [uicolor clearcolor]; navilable.font = [uifont systemfontofsize:16]; navilabel.textalignment = nstextalignmentcenter; [self.view addsubview: navilable]; } -(void)scrollviewdidscroll:(uiscrollview *)scrollview { //navigationbar change cgfloat minalphaoffset = 0; cgfloat maxalphaoffset = 40; cgfloat offset = scrollview.contentoffset.y; cgfloat alpha = (offset - minalphaoffset) / (maxalphaoffset - minalphaoffset); _navibar.alpha = alpha; navilabel.alpha = alpha; }