返回之后导航栏底线ios
2016-08-04 18:08:00 weixin_34112030 阅读数 2

 

 

根据产品需求要求把这个界面导航栏的底线去掉,下个控制器还需要有底线.

 

使用下面的代码实现

 

 

//在页面出现的时候就将黑线隐藏起来

-(void)viewWillAppear:(BOOL)animated

{

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    _navBarHairlineImageView.hidden = YES;

}

//在页面消失的时候就让navigationbar还原样式

-(void)viewWillDisappear:(BOOL)animated{

    

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:nil];

    //再定义一个imageview来等同于这个黑线

    //UIImageView *navBarHairlineImageView;

    _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

 

}

//这个方法唯一的不好就是会影响导航栏的translucent(透明)属性

 

//方法2:找出黑线,再做处理:

//通过一个方法来找到这个黑线(findHairlineImageViewUnder):

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {

    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {

        return (UIImageView *)view;

    }

    for (UIView *subview in view.subviews) {

        UIImageView *imageView = [self findHairlineImageViewUnder:subview];

        if (imageView) {

            return imageView;

        }

    }

    return nil;

}

 

////同样的在界面出现时候开启隐藏

//-(void)viewWillAppear:(BOOL)animated

//{

//    _navBarHairlineImageView.hidden = YES;

//}

////在页面消失的时候就让出现

-(void)viewDidAppear:(BOOL)animated

{

    _navBarHairlineImageView.hidden = NO;

}

转载于:https://www.cnblogs.com/supersr/p/5737772.html

2017-10-25 14:42:57 helloworld183 阅读数 722

 

根据产品需求要求把这个界面导航栏的底线去掉,下个控制器还需要有底线.

 

使用下面的代码实现

 

 

//在页面出现的时候就将黑线隐藏起来

-(void)viewWillAppear:(BOOL)animated

{

    [self.navigationController.navigationBar setBackgroundImage:[UIImage newforBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    _navBarHairlineImageView.hidden = YES;

}

//在页面消失的时候就让navigationbar还原样式

-(void)viewWillDisappear:(BOOL)animated{

    

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:nil];

    //再定义一个imageview来等同于这个黑线

    //UIImageView *navBarHairlineImageView;

    _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

 

}

//这个方法唯一的不好就是会影响导航栏的translucent(透明)属性

 

//方法2:找出黑线,再做处理:

//通过一个方法来找到这个黑线(findHairlineImageViewUnder):

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {

    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {

        return (UIImageView *)view;

    }

    for (UIView *subview in view.subviews) {

        UIImageView *imageView = [self findHairlineImageViewUnder:subview];

        if (imageView) {

            return imageView;

        }

    }

    return nil;

}

 

////同样的在界面出现时候开启隐藏

//-(void)viewWillAppear:(BOOL)animated

//{

//    _navBarHairlineImageView.hidden = YES;

//}

////在页面消失的时候就让出现

-(void)viewDidAppear:(BOOL)animated

{

    _navBarHairlineImageView.hidden = NO;

}


2016-08-04 18:08:00 weixin_33965305 阅读数 2

 

 

根据产品需求要求把这个界面导航栏的底线去掉,下个控制器还需要有底线.

 

使用下面的代码实现

 

 

//在页面出现的时候就将黑线隐藏起来

-(void)viewWillAppear:(BOOL)animated

{

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    _navBarHairlineImageView.hidden = YES;

}

//在页面消失的时候就让navigationbar还原样式

-(void)viewWillDisappear:(BOOL)animated{

    

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:nil];

    //再定义一个imageview来等同于这个黑线

    //UIImageView *navBarHairlineImageView;

    _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

 

}

//这个方法唯一的不好就是会影响导航栏的translucent(透明)属性

 

//方法2:找出黑线,再做处理:

//通过一个方法来找到这个黑线(findHairlineImageViewUnder):

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {

    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {

        return (UIImageView *)view;

    }

    for (UIView *subview in view.subviews) {

        UIImageView *imageView = [self findHairlineImageViewUnder:subview];

        if (imageView) {

            return imageView;

        }

    }

    return nil;

}

 

////同样的在界面出现时候开启隐藏

//-(void)viewWillAppear:(BOOL)animated

//{

//    _navBarHairlineImageView.hidden = YES;

//}

////在页面消失的时候就让出现

-(void)viewDidAppear:(BOOL)animated

{

    _navBarHairlineImageView.hidden = NO;

}

转载于:https://www.cnblogs.com/supersr/p/5737772.html

2016-10-10 17:31:50 luobosiji 阅读数 1779

心血来潮 整理了一下关于UINavigationController的一些总结

首先来说一些细节问题:
1. 系统默认的返回按钮例如如果是从首页跳转则这里写图片描述
如果跳转的页面title过长则为这里写图片描述
如果我们想更改文字的话可以在导航栏栈中的上一级中进行如下设置
(例图中 从首页跳转到1控制器 则在首页中进行设置)

// 更改文字为返回(如果想去掉文字 则title输入空字符串)
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil  action:nil];    
//更改导航栏渲染颜色
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
//当然  如果需要进行全局统一返回样式 则可以创建基类在基类中实现 子控制器继承即可

2.隐藏导航栏及下边线

 //隐藏导航栏并  
 [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  //去掉导航栏下边线 
[self.navigationController.navigationBar setShadowImage:[UIImage new]];

3..接下来讨论下送iOS7开始 系统自带的侧滑返回手势

//  设置禁止导航栏自带的边界右滑返回手势 (默认是YES)
 self.navigationController.interactivePopGestureRecognizer.enabled = NO;

有两个细节

    //1.如果自定义左侧导航栏按钮  则系统自带右滑返回失效
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeft)];

    //自定义左侧导航栏按钮后 右滑返回失效解决方法(设置代理为本控制器)
self.navigationController.interactivePopGestureRecognizer.delegate = self;
// 细节2 如果使用这种方式隐藏导航栏  则系统自带右滑返回失效
   self.navigationController.navigationBarHidden = YES;
   //解决方案使用如下方式
   self.navigationController.navigationBar.hidden = YES;

4.来研究一下全局返回手势

//获取手势系统手势
    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
    gesture.enabled = NO;
    UIView *gestureView = gesture.view;
    NSLog(@"%@",gesture);
//创建返回手势
    UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
    popRecognizer.delegate = self;
    popRecognizer.maximumNumberOfTouches = 1;
    //添加到当前视图上
    [gestureView addGestureRecognizer:popRecognizer];
//  获取系统手势的target数组
    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
     NSLog(@"%@",_targets);
    // 获取它的Target
    id gestureRecognizerTarget = [_targets firstObject];
    id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];

//  通过前面的打印,我们从控制台获取出来它的方法名。
    SEL handleTransition = NSSelectorFromString(@"handleNavigationTransition:");
//添加到自己新添加的手势上作为监听者
    [popRecognizer addTarget:navigationInteractiveTransition action:handleTransition];

这是 获取手势后打印的结果
这里写图片描述
也可以通过如下方式打印出成员变量名
这里写图片描述

全局手势要注意
1.判断是否是跟控制器
2.判断当前是否正在进行转场动画
实现UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}
2015-07-22 14:36:00 weixin_30784501 阅读数 2

前言:
ios7开始 苹果增加了页面 右滑返回的效果;具体的是以UINavigationController为容器的ViewController间右滑切换页面。
代码里的设置是:

self.navigationController.interactivePopGestureRecognizer.enabled = YES;(default is YES)

可以看到苹果给navigationController添加了一个手势(具体为UIScreenEdgePanGestureRecognizer(边缘手势,同样是ios7以后才有的)),就是利用这个手势实现的 ios7的侧滑返回。

问题1:
然而事情并非我们想的那么简单。
1.当我们用系统的UINavigationController,并且也是利用系统的navigateBar的时候,是完全没有问题的
2.但是当我们没有用系统的navigateBar或者自定义了返回按钮的时候,这个时候 右滑返回是失效的。

解决(问题1)办法:
对于这种失效的情况,考虑到interactivePopGestureRecognizer也有delegate属性,替换默认的self.navigationController.interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。

我们可以在NavigationController中设置一下:
self.navigationController.interactivePopGestureRecognizer.delegate =(id)self

问题2
但是出现很多问题,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;push了多层后,快速的触发两次手势,也会错乱

解决(问题2)办法:

@interface NavRootViewController : UINavigationController
@property(nonatomic,weak) UIViewController* currentShowVC;
@end
  
@implementation NavRootViewController
-(id)initWithRootViewController:(UIViewController *)rootViewController
{
NavRootViewController* nvc = [super initWithRootViewController:rootViewController];
self.interactivePopGestureRecognizer.delegate = self;
nvc.delegate = self;
return nvc;
}
 
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (navigationController.viewControllers.count == 1)
self.currentShowVC = Nil;
else
self.currentShowVC = viewController;
}
 
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return (self.currentShowVC == self.topViewController); //the most important
}
return YES;
}
 
@end
借鉴了别人的方法:具体是通过 获取当前pushView栈里的当前显示的VC,根据这个VC来决定 是否开启手势(如果currentShowVC 是当前显示的,则开启手势;如果 currentShowVC为nil,则代表在主页面,关闭手势)
注:
当时试了一种方法 就是滑动的时候来回设置 interactivePopGestureRecognizer的delegate;发现 会有crash,原因就是 因为 我把 当前显示的VC设置为了这个手势的delegate,但当这个VC消失的时候,这个delegate便被释放了,导致crash

至此,觉得ios7上的右滑返回大功告成了,心里正happy,妈蛋,发现了一个可耻的bug:
UIScrollView 上 右滑返回的手势失灵了,靠!!!!!!

问题三:

UIScrollView上手势失灵:
经研究,发现是
UIScrollView上已经添加了 panGestureRecognizer(滑动)手势

ios7 <wbr>侧滑返回

解决(问题三)办法:
参考:http://www.cnblogs.com/lexingyu/p/3702742.html

【解决方案】

苹果以UIGestureRecognizerDelegate的形式,支持多个UIGestureRecognizer共存。其中的一个方法是:

1 // called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other 
2 // return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously) 
3 // 
4 // note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

 一句话总结就是此方法返回YES时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。

 @implementation UIScrollView (AllowPanGestureEventPass)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
        && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
    {
        return YES;
    }
    else
    {
        return  NO;
    }
}

事实上,对UIGestureRecognizer来说,它们对事件的接收顺序和对事件的响应是可以分开设置的,即存在接收链和响应链。接收链如上文所述,和UIView绑定,由UIView的层次决定接收顺序。

而响应链在apple君的定义下,逻辑出奇的简单,只有一个方法可以设置多个gestureRecognizer的响应关系:

// create a relationship with another gesture recognizer that will prevent this gesture's actions from being called until otherGestureRecognizer transitions to UIGestureRecognizerStateFailed // if otherGestureRecognizer transitions to UIGestureRecognizerStateRecognized or UIGestureRecognizerStateBegan then this recognizer will instead transition to UIGestureRecognizerStateFailed // example usage: a single tap may require a double tap to fail - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

每个UIGesturerecognizer都是一个有限状态机,上述方法会在两个gestureRecognizer间建立一个依托于state的依赖关系,当被依赖的gestureRecognizer.state = failed时,另一个gestureRecognizer才能对手势进行响应。

所以,只需要

[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:screenEdgePanGestureRecognizer];

 - (UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer
{
    UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = nil;
    if (self.view.gestureRecognizers.count > 0)
    {
        for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
        {
            if ([recognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
            {
                screenEdgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)recognizer;
                break;
            }
        }
    }

    return screenEdgePanGestureRecognizer;
}

参考:

牛逼 解决了iOS7下滑动返回与ScrollView共存
http://www.cnblogs.com/lexingyu/p/3702742.html

更牛逼  解决了interactivePopGestureRecognizer.delegate 的 释放导致crash的问题
http://www.2cto.com/kf/201401/272886.html

转载于:https://www.cnblogs.com/huangwenan/p/4667396.html

没有更多推荐了,返回首页