for (UIView *view in headerView.subviews) { [view removeFromSuperview]; }
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
- (UINavigationController*)viewController { for (UIView* next = [self superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { return (UINavigationController*)nextResponder; } } return nil; } - (UIViewController*)viewControllerSelf{ for (UIView* next = [self superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { return (UIViewController*)nextResponder; } } return nil; }
代碼以下:html
- (void)setNavigationBar { // 建立NavigationBar UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreeFrame.size.width, 64)]; navigationBar.backgroundColor = [UIColor whiteColor]; // 添加按鈕 (左右都可 例子爲左側返回按鈕) UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, ScreeFrame.size.width / 6, 40); backButton.titleLabel.font = [UIFont systemFontOfSize:15]; [backButton setTitle:@"戻る" forState:UIControlStateNormal]; [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; // 添加點擊事件 [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside]; // 建立 NavigationItem UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"分類"]; UIBarButtonItem *leftButtton = [[UIBarButtonItem alloc] initWithCustomView:backButton]; // 將自定義的按鈕賦給Item的左側按鈕 item.leftBarButtonItem = leftButtton; // 顯示NavigationBar [navigationBar pushNavigationItem:item animated:YES]; [self addSubview:navigationBar]; }
iOS7之前:程序員
navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor];
iOS7之後:設計模式
navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
iOS7以後:設置Title顏色及字體大小,都須要用到:NSForegroundColorAttributeName數組
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:14], NSForegroundColorAttributeName, nil]];
注:app
屢次打印檢測出以下方法返回的數組元素個數爲3(是由於這個方法是 Visible 顯示在屏幕上可見的視圖 )ide
// 返回值是NSIndexPath的數組 [self.collectionView_Subject indexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader]
本部份內容原博:http://blog.it985.com/8776.html函數
時間轉換爲時間戳:字體
NSDate *date = [NSDate date]; NSLog(@"當前日期爲:%@",date); NSTimeInterval timeStamp= [date timeIntervalSince1970]; NSLog(@"日期轉換爲時間戳 %@ = %f", date, timeStamp);
時間戳轉換爲時間:spa
NSString *timeStamp2 = @"1414956901"; long long int date1 = (long long int)[timeStamp2 intValue]; NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1]; NSLog(@"時間戳轉日期 %@ = %@", timeStamp2, date2);
獲取當前時間:.net
NSDate *currentDate = [NSDate date];//獲取當前時間,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; NSLog(@"dateString:%@",dateString);
NSEnumerator *enumerator=[self.segment.subviews reverseObjectEnumerator];//獲得集合的倒序迭代器 id obj = nil; NSInteger i = 0; while(obj = [enumerator nextObject]){ if (i < count - 3 ) { [self.segment.subviews[i] removeFromSuperview]; // NSLog(@"BBB %ld", self.segment.subviews.count); } i++; }
button.selected 默認爲NO
button或者imageView設置圖片圓角:在Xcode7以上:(強調clipsToBounds這個屬性)
// 這個屬性只是習慣加上去了 不知道去了可不能夠設置圓角(不加也能夠的 ~ ) [detailButton layoutIfNeeded]; // 這個屬性很重要 設置以後才能變成圓角 detailButton.clipsToBounds = YES; detailButton.layer.cornerRadius = detailButton.frame.size.width / 8;
原博:http://blog.csdn.net/leikezhu1981/article/details/7446001
http://blog.csdn.net/chengyingzhilian/article/details/7930398
默認值爲YES。
automaticallyAdjustsScrollViewInsets根據按所在界面的status bar,navigationbar,與tabbar的高度,自動調整scrollview的 inset,通常狀況下須要程序員手動設置爲NO, 不讓viewController 調整。[button setImage:[[UIImage imageNamed:@"2.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
imageWithRenderingMode:,它使用UIImageRenderingMode枚舉值來設置圖片的renderingMode屬性。該枚舉中包含下列值:
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *index = [self.tableView_Follow indexPathForCell:cell];
計算UILabel文字高度的時候,
使用方法:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context實現
NSStringDrawingOptions:若是是在多行的狀況下,至少要包括:NSStringDrawingUsesLineFragmentOrigin、NSStringDrawingUsesFontLeading
- (CGSize)labelAutoWithText:(NSString *)text FontSize:(NSInteger)fontSize MaxSize:(CGSize)maxSize{ NSDictionary *dic = @{ NSFontAttributeName:[UIFont systemFontOfSize:14]//字號的名字 }; return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:dic context:nil].size; }
原博地址:http://my.oschina.net/caijunrong/blog/510701
Key Value Observing, 顧名思義就是一種observer 模式用於監聽property的變化,KVO跟NSNotification有不少類似的地方, 用addObserver:forKeyPath:options:context:去start observer, 用removeObserver:forKeyPath:context去stop observer, 回調就是observeValueForKeyPath:ofObject:change:context:。
KVO 的實現也依賴於 Objective-C 強大的 Runtime 。
1> 將系統NavigationBar上的返回按鈕的文字去掉(文字是前一個控制器的title) 只留下箭頭
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-500, 0) forBarMetrics:UIBarMetricsDefault];
2> 給backBarButtonItem設置圖片:
// 返回按鈕 UIImage *img = [UIImage imageNamed:@"back"]; img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.navigationController.navigationBar.backIndicatorImage = img; self.navigationController.navigationBar.backIndicatorTransitionMaskImage = img; self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; // 將後面的文字偏移,進行隱藏 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-500, 0) forBarMetrics:UIBarMetricsDefault];
barTintColor:bar的背景色
TintColor:按鈕的顏色
。。