[英]Changing device orientation settings by Device in Rubymotion
我有一個使用Rubymotion的通用應用程序,並且iPhone版本和iPad版本之間的UI設置差異很大。
最主要的是,在iPhone中,它僅支持人像,而iPad僅支持風景。
我想通過在Rakefile上進行一些設置來做到這一點,據我所知,替代方法是在delagate方法中進行某些操作,但是如果可能的話,我想在設置文件中設置方向設置。
Motion::Project::App.setup do |app|
#app settings
app.name = 'xxxxx'
app.version = "0.xxxx"
app.device_family = [:iphone, :ipad]
app.interface_orientations = [:portrait]
end
編輯:
有關Jamon的答案的一些最新信息。 UINavigation中的shouldAutorotate返回false會導致iPad版本出現奇怪的情況,即使將方向設置為橫向,iPad版本的某些部分也會以縱向顯示其視圖內容,當我為shouldAutorotate返回true時,它可以解決。
據我所知,您將無法在Rakefile中做到這一點。 您需要指定同時提供兩個方向,然后以編程方式告訴iOS是否支持方向。
您的UIViewController和/或UINavigationController應該看起來像這樣:
def ipad?
NSBundle.mainBundle.infoDictionary["UIDeviceFamily"].include?("2")
end
def shouldAutorotate
false # I think?
end
def supportedInterfaceOrientations
if ipad?
UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
else
UIInterfaceOrientationMaskPortrait
end
end
def preferredInterfaceOrientationForPresentation
ipad? ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait
end
尚未測試此代碼,但我過去使用過類似的代碼。 您可以使用三進制(就像我在上一個方法中所做的那樣)或常規if
。
截止到今天,以下作品。
app_delegate.rb:
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = AppController.alloc.initWithNibName(nil, bundle: nil)
@window.makeKeyAndVisible
true
end
end
AppController.rb(或您想要給它的任何名稱):
class AppController < UIViewController
def viewDidLoad
view.backgroundColor = UIColor.blackColor
@label = UILabel.alloc.initWithFrame( CGRectMake(100,100,200,50) )
@label.textColor = UIColor.whiteColor
@label.text = "Handling device orientation"
view.addSubview(@label)
end
def ipad?
return true if UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
end
def shouldAutorotate
false
end
def supportedInterfaceOrientations
if ipad?
UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
else
UIInterfaceOrientationMaskPortrait
end
end
def preferredInterfaceOrientationForPresentation
ipad? ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait
end
end
AppController中添加的標簽僅給出了視覺提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.