【发布时间】:2014-08-13 10:32:23
【问题描述】:
旨在实现一个仅在我修改didFinishLaunchingWithOptions 后才显示的初始屏幕,以便动态选择适当的视图控制器。逻辑似乎运行良好,我打算加载的视图是已启动的视图
但是,如果我没有更改 didFinishLaunchingWithOptions 函数,UI 似乎缺少原本会显示的元素。
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool
{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
var entryViewController: UIViewController?
if NSUserDefaults.standardUserDefaults().boolForKey("hasSeenWelcomeScreen") == true
{
entryViewController = storyBoard.instantiateViewControllerWithIdentifier("NavigationController") as? UIViewController
}
else
{
entryViewController = storyBoard.instantiateViewControllerWithIdentifier("WelcomeViewController") as? UIViewController
NSUserDefaults.standardUserDefaults().setValue(true, forKey: "hasSeenWelcomeScreen")
NSUserDefaults.standardUserDefaults().synchronize()
}
self.window?.rootViewController = entryViewController
self.window?.makeKeyAndVisible()
return true
}
我的WelcomeViewController 是一个简单的视图,其中1 label、1 button 和a movie 在后台播放(类似于Spotify/Vine 的欢迎屏幕)。调试代码我可以看到初始化方法确实被执行了,但只是当我动态覆盖初始视图
import UIKit
import MediaPlayer
import QuartzCore
class WelcomeViewController: UIViewController {
var moviePlayerController: MPMoviePlayerController = MPMoviePlayerController()
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var appNameLabel: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
buildMoviePreview()
buildButtonDesign()
}
override func viewWillAppear(animated: Bool)
{
self.view.addSubview(self.moviePlayerController.view)
self.view.addSubview(self.loginButton)
self.view.addSubview(self.appNameLabel)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
private func buildButtonDesign()
{
loginButton.layer.borderColor = UIColor.whiteColor().CGColor
loginButton.layer.borderWidth = 2.0
loginButton.layer.cornerRadius = 7.0
}
private func buildMoviePreview()
{
let filePath = NSBundle.mainBundle().pathForResource("intro", ofType: "mov")
self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)
self.moviePlayerController.movieSourceType = .File
self.moviePlayerController.repeatMode = .One
self.moviePlayerController.view.frame = self.view.bounds
self.moviePlayerController.scalingMode = .AspectFill
self.moviePlayerController.controlStyle = .None
self.moviePlayerController.allowsAirPlay = false
self.moviePlayerController.shouldAutoplay = true
self.moviePlayerController.play()
}
}
为了完整起见,这些是使用 XCode UI 调试器时布局中的差异。请注意,即使它们实现相同的 viewController,它们也会有所不同。唯一的区别是一个被编程设置为initial view,而另一个被设置为initial view通过storyboard。
并排呈现问题的屏幕截图
【问题讨论】:
-
请添加问题截图。
-
@LeoNatan,完成。干杯。
标签: ios uiview uiviewcontroller swift mpmovieplayercontroller