常用解決方案匯總

1、如果子視圖想push一個(gè)控制器,而子視圖中拿不到navigationController 怎么辦
2、圖片的拉伸,當(dāng)圖片被拉伸后,當(dāng)前空缺的地方是copy這個(gè)設(shè)置的像素點(diǎn)
3、 KVO(觀察者)的使用。
4、collectionView的邊緣值,我用的xcode7反正這個(gè)方法我是沒找到
5、用storyboard創(chuàng)建的cell是不用注冊(cè)也不用判斷是否為空

1、 如果子視圖想push一個(gè)控制器,而子視圖中拿不到navigationController 怎么辦

解決辦法一

//取出根視圖控制器
    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    //取出當(dāng)前選中的導(dǎo)航控制器
    UINavigationController *Nav = [tabBarVc selectedViewController];
    [Nav pushViewController:topDetailView animated:YES];

解決辦法二

// 給UIView 寫個(gè)分類
/** 實(shí)現(xiàn)子視圖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、 圖片的拉伸,當(dāng)圖片被拉伸后,當(dāng)前空缺的地方是copy這個(gè)設(shè)置的像素點(diǎn)

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

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

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

// 添加觀察者
        [self.smallView addObserver:self
               forKeyPath:@"index" // 監(jiān)聽的值
                  options:NSKeyValueObservingOptionNew
                  context:nil];

#pragma mark - 觀察者
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)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 - 手動(dòng)移除觀察者
- (void) dealloc {
    [self.smallView removeObserver:self forKeyPath:@"index"];
}

4、collectionView的邊緣值,我用的xcode7反正這個(gè)方法我是沒找到

#pragma mark - UICollectionViewDelegateFlowLayout

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(0, 50, 0, 50);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容