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];?
4:模擬器改為Ipad?userInterfaceIdiom沒有效果的解決方法?
將模擬器改為Ipad時,調用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判斷設備是否為Ipad,但程序并未做出正確的判斷,后來做出如下設置:
PROJECT->Build Settings->Deployment->Targeted Device Family->iPhone/iPad
再次調試程序,成功識別出設備
轉載于:https://www.cnblogs.com/wujy/p/5481622.html
總結
以上是生活随笔為你收集整理的IOS开发基础知识--碎片39的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SCTF 2015 pwn试题分析
- 下一篇: UITextView实现PlaceHol