文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

iOS滑动全屏实现返回功能

2022-06-02 16:04

关注

本文实例为大家分享了iOS滑动全屏实现返回功能的具体代码,供大家参考,具体内容如下

系统自带的滑动返回功能,只能滑动边缘返回,而我们希望通过滑动全屏实现返回功能。

定义BaseNavigationController来取代UINavigationController


// BaseNavigationController.h
#import <UIKit/UIKit.h>

@interface BaseNavigationController : UINavigationController

@end

// BaseNavigationController.m
#import "BaseNavigationController.h"

@interface BaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController

#pragma mark - 系统方法
+ (void)load
{
 UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];

 // 只要是通过模型设置,都是通过富文本设置
 // 设置导航条标题 => UINavigationBar
 NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
 attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20.0];
 [navBar setTitleTextAttributes:attrs];

 // 设置导航条背景图片
 [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.

 // 自己控制返回手势
// self.interactivePopGestureRecognizer.delegate = self;
// NSLog(@"self.interactivePopGestureRecognizer : %@",self.interactivePopGestureRecognizer);
// NSLog(@"self.interactivePopGestureRecognizer.delegate : %@",self.interactivePopGestureRecognizer.delegate);

 // 全屏返回手势,而不是边缘返回手势
 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
 [self.view addGestureRecognizer:pan];
 // 控制手势什么时候触发,只有非根控制器才需要出发手势
 pan.delegate = self;
 // 禁止之前手势
 self.interactivePopGestureRecognizer.enabled = NO;
}



#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
 // 当不是根控制器时才会触发返回手势
 return (self.childViewControllers.count > 1);
}

#pragma mark - 重写
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
 if (self.childViewControllers.count > 0) { // 代表不是根控制器

  // 自定义返回按钮覆盖了系统的返回手势
  viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem setBackButtonItemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highlightedImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(backClick) title:@"返回"];
 }

 [super pushViewController:viewController animated:animated];
}

- (void)backClick
{
 [self popViewControllerAnimated:YES];
}

@end

UIBarButtonItem+item


// UIBarButtonItem+item.h
#import <UIKit/UIKit.h>

@interface UIBarButtonItem (item)

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action;

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action;

+ (UIBarButtonItem *)setBackButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action title:(NSString *)title;

@end


// UIBarButtonItem+item.m
#import "UIBarButtonItem+item.h"

@implementation UIBarButtonItem (item)

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action
{
 // 1.leftBarButtonItem
 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [leftButton setImage:image forState:UIControlStateNormal];
 [leftButton setImage:highlightedImage forState:UIControlStateHighlighted];
 [leftButton sizeToFit];

 // 按钮点击事件
 [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

 // 由于使用button会使得点击范围增大,所以将button放进view中,然后赋值
 UIView *leftView = [[UIView alloc] initWithFrame:leftButton.bounds];
 [leftView addSubview:leftButton];
 return [[UIBarButtonItem alloc] initWithCustomView:leftView];
}

+ (UIBarButtonItem *)setBackButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL)action title:(NSString *)title
{
 // 设置返回按钮
 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [backButton setTitle:title forState:UIControlStateNormal];
 [backButton setImage:image forState:UIControlStateNormal];
 [backButton setImage:highlightedImage forState:UIControlStateHighlighted];
 [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
 [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
 backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
 [backButton sizeToFit];

 return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

+ (UIBarButtonItem *)setBarButtonItemWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage target:(id)target action:(SEL)action
{
 // 1.leftBarButtonItem
 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [leftButton setImage:image forState:UIControlStateNormal];
 [leftButton setImage:selectedImage forState:UIControlStateSelected];
 [leftButton sizeToFit];

 // 按钮点击事件
 [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

 // 由于使用button会使得点击范围增大,所以将button放进view中,然后赋值
 UIView *leftView = [[UIView alloc] initWithFrame:leftButton.bounds];
 [leftView addSubview:leftButton];
 return [[UIBarButtonItem alloc] initWithCustomView:leftView];
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯