IOS開發基礎知識--片段39,ios基礎知識--39
1:UIWindow知識點
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; self.window.backgroundColor=[UIColor redColor]; self.window.rootViewController=[[ViewController alloc]init]; [self.window makeKeyAndVisible]; return YES;}
iOS9要求所有UIWindow都有rootViewController,如果沒有rootViewController就會報錯了;
(a)【self.window makekeyandvisible】讓視窗成為主視窗,並且顯示出來。有這個方法,才能把資訊顯示到螢幕上。
(b) 因為Window有makekeyandvisible這個方法,可以讓這個Window憑空的顯示出來,而其他的view沒有這個方法,所以它只能依賴於Window,Window顯示出來後,view才依附在Window上顯示出來。
(c)【self.window make keywindow】//讓uiwindow成為主視窗,但不顯示。
(d)[UIApplication sharedApplication].windows 在本應用中開啟的UIWindow列表,這樣就可以接觸應用中的任何一個UIView對象(平時輸入文字彈出的鍵盤,就處在一個新的UIWindow中)
(d)[UIApplication sharedApplication].keyWindow(擷取應用程式的主視窗)用來接收鍵盤以及非觸摸類的訊息事件的UIWindow,而且程式中每個時刻只能有一個UIWindow是keyWindow。
提示:如果某個UIWindow內部的文字框不能輸入文字,可能是因為這個UIWindow不是keyWindow
(f)view.window獲得某個UIView所在的UIWindow
2:UINavigationController知識點
a.把子控制器添加到導航控制器中的四種方法
第一種:
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav pushViewController:one animated:YES];
第二種:
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav addChildViewController:one];
第三種:
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
nav.viewControllers=@[one];(添加到導航控制器的棧中)
說明:nav.viewControllers;== nav.childViewControllers;注意該屬性是唯讀,因此不能像下面這樣寫。nav.childViewControllers = @[one];
第四種:最常用的方法
YYOneViewController *one=[[YYOneViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
b.當前子控制器介面導覽列的標題以及對應返回標題的設定
self.navigationItem.title=@"第一個介面";
self.navigationItem.backBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"返回一" style:UIBarButtonItemStylePlain target:nilaction:nil];
c.給導覽列添加按鈕
說明:可添加一個,也可以添加多個(數組)
添加導覽列左邊的按鈕(添加一個相機表徵圖的按鈕),會蓋掉返回
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
d.介面跳轉
跳轉到第二個介面(當前為第三個,移除當前棧頂的控制器)
[self.navigationController popViewControllerAnimated:YES];
移除處理棧底控制器之外的所有控制器
[self.navigationController popToRootViewControllerAnimated:YES];
只要傳入棧中的某一個控制器,就會跳轉到指定控制器 [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
說明:
導航控制器是通過棧的形式來管理子控制器的(先進後出),顯示在導航控制器上得view永遠是棧頂控制器的view,一個導航控制器只有一個導航條,也就是說所有的自控制器公用一個導航條。
附:列印當前window下面的所有子控制項,並通過xml檔案來儲存
// 應用程式擷取焦點(代表著可以和使用者互動)- (void)applicationDidBecomeActive:(UIApplication *)application{ NSLog(@"applicationDidBecomeActive"); UINavigationController *nav = (UINavigationController *)self.window.rootViewController; UINavigationBar *bar = nav.navigationBar;// NSLog(@"%@", NSStringFromCGRect(bar.frame)); NSString *str = [self digView:self.window]; [str writeToFile:@"/Users/apple/Desktop/ios6.xml" atomically:YES]; }/** * 返回傳入veiw的所有層級結構 * * @param view 需要擷取層級結構的view * * @return 字串 */- (NSString *)digView:(UIView *)view{ if ([view isKindOfClass:[UITableViewCell class]]) return @""; // 1.初始化 NSMutableString *xml = [NSMutableString string]; // 2.標籤開頭 [xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)]; if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) { [xml appendFormat:@" bounds=\"%@\"", NSStringFromCGRect(view.bounds)]; } if ([view isKindOfClass:[UIScrollView class]]) { UIScrollView *scroll = (UIScrollView *)view; if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) { [xml appendFormat:@" contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)]; } } // 3.判斷是否要結束 if (view.subviews.count == 0) { [xml appendString:@" />"]; return xml; } else { [xml appendString:@">"]; } // 4.遍曆所有的子控制項 for (UIView *child in view.subviews) { NSString *childXml = [self digView:child]; [xml appendString:childXml]; } // 5.標籤結尾 [xml appendFormat:@"</%@>", view.class]; return xml;}
3:UICollectionViewLayout自訂知識點
a:首先查看其開放的屬性及方法,可以使用屬性對應其方法,方法會比較靈活
@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>@optional- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;@endNS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewFlowLayout : UICollectionViewLayout@property (nonatomic) CGFloat minimumLineSpacing;@property (nonatomic) CGFloat minimumInteritemSpacing;@property (nonatomic) CGSize itemSize;@property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes:@property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical@property (nonatomic) CGSize headerReferenceSize;@property (nonatomic) CGSize footerReferenceSize;@property (nonatomic) UIEdgeInsets sectionInset;// Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView).@property (nonatomic) BOOL sectionHeadersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);@property (nonatomic) BOOL sectionFootersPinToVisibleBounds NS_AVAILABLE_IOS(9_0);@end
b:Item size(每個item的大小),可以靈活定義到每個Item的大小
layout.itemSize = CGSizeMake(30,20);
或
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
c:Line spacing(每行的間距)
@property (CGFloat) minimumLineSpacing
或
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
注意:這邊設定為其小的每行間距,實際如果大於這個間距就以自個為準;
d:Inter cell spacing(每行內部cell item的間距)
@property (CGFloat) minimum InteritemSpacing
或
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
注意:同樣設定為最小的間距,實際如果比這個大則以自個為準
e:Scrolling direction(滾動方向)
設定scrollDirection屬性即可。兩個值如下
UICollectionViewScrollDirectionVertical
UICollectionViewScrollDirectionHorizontal
f:Header and footer size(頁首和頁尾大小)
下面是頁首和頁尾的一些解釋。
n 也就是supplementary views
n 通過資料來源的方法來提供內容,如下
- collectionView:viewForSupplementaryElementOfKind:atIndexPath:
n 兩種常量(類型)
UICollectionElementKindSectionHeader
UICollectionElementKindSectionFooter
n 同樣需要註冊一個類並從隊列中取出view
- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
- registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
- dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
頁首和頁尾的size配置方式:
1)可以全域配置,如下屬性
@property (CGSize) headerReferenceSize
@property (CGSize) footerReferenceSize
2)也可以通過delegate對每一個section進行配置,如下代碼
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
當垂直的時候,需要設定Height,當水平的時候,需要設定Width
g:Section Inset(Section Inset就是某個section中cell的邊界範圍)
@property UIEdgeInsets sectionInset;
或
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
運用時:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; self.mediaView = [[UICustomCollectionView alloc] initWithFrame:CGRectMake(10, 10, Main_Screen_Width-2*15, 80) collectionViewLayout:layout];