欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Objective-C实现自定义的半透明导航

程序员文章站 2023-12-02 18:44:10
本文实例为大家分享了objective-c半透明导航的具体实现代码,供大家参考,具体内容如下 #define kscreenwidth [[uiscreen ma...

本文实例为大家分享了objective-c半透明导航的具体实现代码,供大家参考,具体内容如下

#define kscreenwidth [[uiscreen mainscreen] bounds].size.width
#define kscreenheight [[uiscreen mainscreen] bounds].size.height
 
#import "rswennav.h"
 
@implementation rswennav
{
 uivisualeffectview *effectview;
}
-(instancetype)initwithframe:(cgrect)frame{
 self=[super initwithframe:frame];
 if (self) {
  uiblureffect *blur = [uiblureffect effectwithstyle:uiblureffectstylelight];
 effectview = [[uivisualeffectview alloc] initwitheffect:blur];
 effectview.frame=frame;
 [self addsubview:effectview];
 [self addsubviews];
 }
 return self;
}
 
//添加导航子视图
-(void)addsubviews{
 [self addsubview:self.backbtn];
 [self addsubview:self.titlelabel];
 [self addsubview:self.rightbtn];
 
}
-(uibutton *)backbtn{
 uibutton * btn=[uibutton buttonwithtype:uibuttontypecustom];
 btn.frame=cgrectmake(20, 20, 60, 44);
 [btn settitle:@"返回" forstate:uicontrolstatenormal];
 [btn addtarget:self action:@selector(rswennavback) forcontrolevents:uicontroleventtouchupinside];
 return btn;
}
 
-(uilabel *)titlelabel{
 uilabel * lable=[[uilabel alloc]init];
 lable.font=[uifont systemfontofsize:17];
 lable.textalignment=nstextalignmentcenter;
 lable.frame=cgrectmake(100, 20, kscreenwidth-200, 44);
 lable.text=@"我的主页";
 return lable;
}
 
-(uibutton *)rightbtn{
 uibutton * btn=[uibutton buttonwithtype:uibuttontypecustom];
 btn.frame=cgrectmake([uiscreen mainscreen].bounds.size.width-100, 20, 80, 44);
 [btn settitle:@"保存" forstate:uicontrolstatenormal];
 [btn addtarget:self action:@selector(rightbtnclicked) forcontrolevents:uicontroleventtouchupinside];
 return btn;
}
 
#pragma 事件处理部分
 
//返回上个界面
-(void)rswennavback{
 //获取uiview的上层uiviewcontroller
 id object = [self nextresponder];
 while (![object iskindofclass:[uiviewcontroller class]] &&
  
  object != nil) {
  
 object = [object nextresponder];
  
 }
 uiviewcontroller *uc=(uiviewcontroller*)object;
 
 [uc.navigationcontroller popviewcontrolleranimated:yes];
}
 
//右侧按钮被点击
-(void)rightbtnclicked{
 if ([_delegate respondstoselector:@selector(rswennavrightbtnclicked)]) {
 [_delegate rswennavrightbtnclicked];
 }
}
 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助。