iOS全屏的背景,多個按鈕位置適配(swift),iosswift
最近寫部落格比較多,其實大多是之前的隨筆,這一段整理一下,發布出來。
今天這篇文章,說一下,面對全屏的背景圖片,要在固定的位置放置多個按鈕的問題我的解決辦法,其實很土,談不上是技術,就是一種方法,很省力。
圖片就是這樣的。再重複一下問題:例如我要在上述全屏的背景上的 M U R P 和 訪問官方網站五個地方放置五個按鈕,要求適配各種型號手機。
我是這樣解決的(StoryBoard):
首先:在storyBoard裡面拖4個ViewController,分別設定screen size 為3.5、 4.0、 4.7、 5.5的尺寸。然後把背景圖片分別設定上去,在固定的地方放置固定的按鈕。
然後在使用的時候通過判斷不同的螢幕尺寸,來載入不同的storyBoard,代碼如下:(swift)。
1 let screenHeight = UIScreen.mainScreen().bounds.size.height 2 var storyBoards:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) as UIStoryboard 3 if (screenHeight <= 480.0) { 4 5 viewCon = storyBoards.instantiateViewControllerWithIdentifier("First3.5") as! FirstViewController 6 }else if (screenHeight <= 568.0) { 7 8 viewCon = storyBoards.instantiateViewControllerWithIdentifier("First4.0") as! FirstViewController 9 }else if (screenHeight <= 667.0) {10 11 viewCon = storyBoards.instantiateViewControllerWithIdentifier("First4.7") as! FirstViewController12 }else if (screenHeight <= 736.0) {13 14 viewCon = storyBoards.instantiateViewControllerWithIdentifier("First5.5") as! FirstViewController15 }16 17 var naviCon:MainNavigationController = MainNavigationController(rootViewController:viewCon)
這樣一來,就可以全部適配了。
想看代碼的小夥伴:--> github:要Demo的小夥伴看過來