view.traitCollection.horizontalSizeClass在viewDidLoad中返回undefined(0) [英] view.traitCollection.horizontalSizeClass returning undefined (0) in viewDidLoad
问题描述
我在UIPageViewController中使用UITextView,我想根据设备的大小等级确定字体大小。
I'm using a UITextView inside a UIPageViewController, and I want to determine the font size based on the size class of the device.
第一张幻灯片页面视图像ViewDidLoad一样加载( viewControllerAtIndex(0)
):
The first slide of the page view is loaded in ViewDidLoad like so (viewControllerAtIndex(0)
):
override func viewDidLoad() {
super.viewDidLoad()
//Some unrelated code here
// Page View Controller for Questions Slider
questionPageVC = storyboard?.instantiateViewControllerWithIdentifier("QuestionPageView") as? UIPageViewController
questionPageVC!.dataSource = self;
questionPageVC!.delegate = self;
let startingViewController : QuestionContentViewController = viewControllerAtIndex(0) as QuestionContentViewController
var viewControllers = [startingViewController]
questionPageVC!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
let sliderHeight = view.frame.size.height * 0.5
questionPageVC!.view.frame = CGRectMake(20, 70,
view.frame.size.width-40, sliderHeight)
addChildViewController(questionPageVC!)
view.addSubview(questionPageVC!.view!)
questionPageVC?.didMoveToParentViewController(self)
var pageControl : UIPageControl = UIPageControl.appearance()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
pageControl.backgroundColor = UIColor.whiteColor()
// Some more code here
}
然后,在viewControllerAtIndex中:
And then, in viewControllerAtIndex:
private func viewControllerAtIndex(index: Int) -> QuestionContentViewController {
var pcvc : QuestionContentViewController = storyboard?.instantiateViewControllerWithIdentifier("QuestionContentView") as! QuestionContentViewController
var fontSize = ""
if (view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact) {
fontSize = "20"
} else {
fontSize = "28"
}
pcvc.questionString = TextFormatter(string: fontSize + questionsArray[index]).formattedString
pcvc.questionIndex = index
return pcvc
}
问题是第一张幻灯片,在viewDidLoad中调用,总是使用else子句中的字体大小。
The problem is that the very first slide, which was called in viewDidLoad, always uses the font size in the "else" clause.
如果我打印视图.traitCollection.horizontalSizeClass
,对于第一滑动,我得到0( UIUserInterfaceSizeClassUnspecified
),用于随后的幻灯片时,得到正确的尺寸。
If I print view.traitCollection.horizontalSizeClass
, for that first slide, I get 0 (UIUserInterfaceSizeClassUnspecified
), for subsequent slides, I get the correct size.
我尝试将整个事情移动到viewWillAppear,然后UIPageViewController发生了奇怪的事情(另一张幻灯片放在其他幻灯片后面的文字错误)
I tried moving the whole thing to "viewWillAppear", and then weird things happen to the UIPageViewController (an extra slide with the wrong size text behind the other slides)
推荐答案
问题是 viewDidLoad
现在要求查看视图的特征集合还为时过早。这是因为视图的特征集合是从视图层次结构中获取的特征,即它发现自身的环境。但是在 viewDidLoad
中,视图具有 no 环境:它在视图层次结构中不是 。它已加载,意味着它存在:视图控制器现在具有视图。但是它还没有被放到界面中,直到 <$ em> <$ em $ c> c> viewWillAppear:之后才会进入界面。事件序列。
The problem is that viewDidLoad
is too soon to be asking about a view's trait collection. This is because the trait collection of a view is a feature acquired from the view hierarchy, the environment in which it finds itself. But in viewDidLoad
, the view has no environment: it is not in in the view hierarchy yet. It has loaded, meaning that it exists: the view controller now has a view. But it has not been put into the interface yet, and it will not be put into the interface until after viewWillAppear:
, which comes later in the sequence of events.
然而,视图控制器也有一个特征集合, 有一个环境:在调用 viewDidLoad
时,视图控制器是视图控制器层次结构的一部分。因此,最简单(和正确)的解决方案是要求 traitCollection
self
,而不是视图
。只需说 self.traitCollection
你现在有 view.traitCollection
,一切都会好的。
However, the view controller also has a trait collection, and it does have an environment: by the time viewDidLoad
is called, the view controller is part of the view controller hierarchy. Therefore the simplest (and correct) solution is to ask for the traitCollection
of self
, not of view
. Just say self.traitCollection
where you now have view.traitCollection
, and all will be well.
(您的解决方案,询问屏幕的特征收集,可能发生工作,但它不可靠,并且不是正确的方法。这是因为它父视图控制器可以更改其子级的特征集合,如果绕过正确的方法并直接询问屏幕,则无法获得正确的特征集合。)
(Your solution, asking the screen for its trait collection, may happen to work, but it is not reliable and is not the correct approach. This is because it is possible for the parent view controller to alter the trait collection of its child, and if you bypass the correct approach and ask the screen, directly, you will fail to get the correct trait collection.)
这篇关于view.traitCollection.horizontalSizeClass在viewDidLoad中返回undefined(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!