iOS中UIView之間布局及跳轉的幾種方式

來源:互聯網
上載者:User

iOS中UIView之間布局及跳轉的幾種方式

UIView是iOS開發中所有視圖的基類, 表示螢幕上的一塊矩形地區, 同時可以處理該地區的繪製和觸摸事件. UIViewController是視圖控制器的基類, 用來處理螢幕之間的切換等操作, 提供視圖管理模型. 一個UIViewController管理一個層級的UIView. 而RootViewController就是iOS應用啟動時被載入的第一個視圖控制器(可在main.storyboard中指定), 展示APP啟動成功後的第一個介面. 因此, iOS中在各個UIViewController之間的切換操作顯得尤為重要, 其直接決定了應用各個介面之間的轉場效果. 主要的跳轉方式有以下幾種:

UITabBarController

UITabBarController主要用於平級View的跳轉, 應用案例如介面下方的四個Tab。可選中一個UIViewController, 在Xcode->Editor->Embed In-> Tab Bar Controller將其加入到一個Tab bar中. 也可以在storyboard中直接從UITabBarController中連線至一個View, 選擇Relationship Segue的view controllers即可.

property

tabbar裡邊包含的每一個viewController都對應一個tabbarItem, 位置都是均分的, 最多顯示4個tab, 再多了就會摺疊起來. 通過代碼來設定tabbar的時候, 可以使用setViewControllers來添加指定的子ViewController為其item.
tabbarItem有title, image, selectedImage, badgeValue屬性, badgeValue是該item右上方的提醒數字. 而selectedIndex和selectedViewController則定位到當前選取的tabbarItem.
除此之外, 還有viewControllers, selectedViewController, selectedIndex等屬性, 含義就不羅嗦了.

UITabBarControllerDelegate

該協議用於在選取某一個tabbarItem的時候, 執行一些額外的操作, 監控tabbar的改變, 也可以阻止某一個tabbarItem被選取.

UINavigationController堆棧式View管理

UINavigationController是IOS開發中常用的用於視圖切換的控制器, 提供堆棧式的View管理方式, RootViewController在stack的最底層. 提供了諸多方法用於進行view之間的切換及管理等, 如
pushViewController與popViewController等. 詳細內容, 可參考之前的一篇部落格UINavigationController的簡單總結.
一般使用UINavigationController的方式, 會自動為我們設定好每個View介面的標題, 左上方的返回按鈕, 以及螢幕右滑回退的操作. 如果想要禁止螢幕右滑返回等的手勢操作, 可以在當前View的viewDidAppear方法中設定如下:

self.navigationController.interactivePopGestureRecognizer.enabled = NO; // 禁止右滑等手勢

需要注意的是 UINavigationController是採用類似stack的push和pop的方式完成view的切換, 調用方法為pushViewController和popViewController. 而segue屬性也要相應地設定為push.
使用viewControllers屬性可以擷取當前的視圖棧.

property

toolbarHidden是用於隱藏navigationController最上方的導航工具列. 在該工具列中, 我們可以自行添加各種Bar Button Item控制項. 常見的是leftBarButtonItem和rightBarButtonItem.
UINavigationItem是該View棧中的每一項. 可以在Storyboard或xib檔案中指定, 也可以自行代碼建立, 然後加到UINavigationController中去即可.

UINavigationControllerDelegate

該協議為NavigationController中的View跳轉, 提供了很多遍曆的方法.如: didShowViewController, willShowViewController, animationControllerForOperation等. 而枚舉UINavigationControllerOperation中定義了View之間跳轉的方式(None, Push, Pop).

使用nib檔案

nib檔案是一系列UIView的組合.

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];self.vMessageCenter = [arrayMessage objectAtIndex:0];self.vMessageCenter.navigationController = self.navigationController;self.vMessageCenter.frame = self.vMainPanel.bounds;
使用storyboard

將一個ViewController放在storyboard中, 然後調用instantiateViewControllerWithIdentifier, 載入一個storyboard檔案中的對應ID的storyboard(一系列view的集合), 也是非常常用的一種方式.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@“Main” bundle:nil];LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@“LoginViewController”];[self.navigationController popToRootViewControllerAnimated: YES];[self.navigationController presentViewController: vc animated: YES completion:nil];
使用nib檔案

Nib檔案是一種特殊類型的資源檔, 儲存Interface Builder文檔, 可以進行可視化編輯.
每一個xib檔案對應一個ViewController或者一個自訂的View, 可以使用loadNibNamed:方法來載入nib檔案

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];self.vMessageCenter = [arrayMessage objectAtIndex:0];self.vMessageCenter.navigationController = self.navigationController;self.vMessageCenter.frame = self.vMainPanel.bounds;
segue

對於兩個單獨的ViewController, 可以使用segue指定跳轉方式.
如在storyboard中, 在VC1中的button上右鍵, 連線至第二個VC, 選擇跳轉方式即可實現兩個VC之間的相互跳轉.
如果想通過點擊一個image, 實現VC的跳轉呢? 這就要引入gesture了.

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

View載入的時候設定手勢:

self.imageView.userInteractionEnabled = YES;UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];[self.imageView addGestureRecognizer:imageTap];
通過addSubView

如self.view.addSubView(newView) 即可直接載入UIView, 使用removeFromSuperview將該UIView移除.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.