欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)

 更新時間:2016年09月17日 10:59:48   作者:Silktree  
這篇文章主要介紹了Runtime解決屏幕旋轉(zhuǎn)問題的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

前言

大家或許在iOS程序開發(fā)中經(jīng)常遇到屏幕旋轉(zhuǎn)問題,比如說希望指定的頁面進(jìn)行不同的屏幕旋轉(zhuǎn),但由于系統(tǒng)提供的方法是導(dǎo)航控制器的全局方法,無法隨意的達(dá)到這種需求。一般的解決方案是繼承UINavrgationViewController,重寫該類的相關(guān)方法,這樣雖然也能解決問題,但是在重寫的過程中至少產(chǎn)生兩個多余的文件和不少的代碼,這顯然不是我們想要的。下面就使用一種較底層的方法解決這個問題。

基本原理

動態(tài)的改變UINavrgationViewController的全局方法,將我們自己重寫的supportedInterfaceOrientations、shouldAutorotate方法和導(dǎo)航控制器對象的方法進(jìn)行替換即可。

準(zhǔn)備工作

配置項目支持方向

代碼實現(xiàn)

將下面的方法寫在所有視圖控制器的父類的viewDidLoad方法中,即可完成屏幕旋轉(zhuǎn)方向的配置。

//獲取當(dāng)前視圖控制器的旋轉(zhuǎn)支持方法
Method selfMtihod = class_getInstanceMethod([self class], @selector(shouldAutorotate));
//獲取當(dāng)前導(dǎo)航控制器的旋轉(zhuǎn)支持方法
Method navr = class_getInstanceMethod([self.navigationController class], @selector(shouldAutorotate));
//交換方法
method_exchangeImplementations(selfMtihod, navr);
//以下同理
Method selfOrientation = class_getInstanceMethod([self class], @selector(supportedInterfaceOrientations));
Method navrOrientation = class_getInstanceMethod([self.navigationController class], @selector(supportedInterfaceOrientations));
method_exchangeImplementations(selfOrientation, navrOrientation);

使用方法

在上面的父類中重寫supportedInterfaceOrientations、shouldAutorotate,表示默認(rèn)的屏幕旋轉(zhuǎn)相關(guān)屬性。

在之后的每個該試圖控制器的子類中,可重寫supportedInterfaceOrientations、shouldAutorotate方法,即可完成指定視圖控制器方向的需求。

以上所述是小編給大家介紹的Runtime解決屏幕旋轉(zhuǎn)問題的方法詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論