常用解决方案汇总

1、如果子视图想push一个控制器,而子视图中拿不到navigationController 怎么办
2、图片的拉伸,当图片被拉伸后,当前空缺的地方是copy这个设置的像素点
3、 KVO(观察者)的使用。
4、collectionView的边缘值,我用的xcode7反正这个方法我是没找到
5、用storyboard创建的cell是不用注册也不用判断是否为空

1、 如果子视图想push一个控制器,而子视图中拿不到navigationController 怎么办

解决办法一

//取出根视图控制器
    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    //取出当前选中的导航控制器
    UINavigationController *Nav = [tabBarVc selectedViewController];
    [Nav pushViewController:topDetailView animated:YES];

解决办法二

// 给UIView 写个分类
/** 实现子视图push到控制器 */
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}


// 然后就可以这样使用
[self.viewController.navigationController pushViewController:topDetailView animated:YES];

2、 图片的拉伸,当图片被拉伸后,当前空缺的地方是copy这个设置的像素点

// 图片拉伸
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];

    UIImage *stresImage = [image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    
    headView.image = stresImage;

3、KVO(观察者)的使用。

// 添加观察者
        [self.smallView addObserver:self
               forKeyPath:@"index" // 监听的值
                  options:NSKeyValueObservingOptionNew
                  context:nil];

#pragma mark - 观察者
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    NSInteger index = [[change objectForKey:@"new"]integerValue];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    
    
    [UIView animateWithDuration:0.35 animations:^{
        
        [self.posterView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }];
    
}

#pragma mark - 手动移除观察者
- (void) dealloc {
    [self.smallView removeObserver:self forKeyPath:@"index"];
}

4、collectionView的边缘值,我用的xcode7反正这个方法我是没找到

#pragma mark - UICollectionViewDelegateFlowLayout

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(0, 50, 0, 50);
}

你可能感兴趣的:(常用解决方案汇总)