UICollectionViewCell到UIButton专注于tvOS
我有一个包含12-13 UICollectionViewCells的UICollectionView。 我可以轻松关注UICollectionViewCells,一切正常。 在UICollectionView之外有一个UIButton。 如果我在第一个单元格或第二个单元格,然后向上滑动,则可以轻松地将焦点放在UIButton上。 当我在第三个单元格上时,我无法将焦点移到UIButton上。 任何想法是怎么回事?
你应该使用包含你想要关注的UIButton的UIFocusGuide。 使UIFocusGuide与collectionView一样宽,并且足够高以覆盖button。 使preferredFocusViewbutton。
UIFocusGuide *topButtonFocusGuide = [[UIFocusGuide alloc] init]; topButtonFocusGuide.preferredFocusedView = myButton; [self.view addLayoutGuide:topButtonFocusGuide]; [self.view addConstraints:@[ [topButtonFocusGuide.topAnchor constraintEqualToAnchor:myButton.topAnchor], [topButtonFocusGuide.bottomAnchor constraintEqualToAnchor:myCollectionView.topAnchor], [topButtonFocusGuide.leadingAnchor constraintEqualToAnchor:myCollectionView.leadingAnchor], [topButtonFocusGuide.widthAnchor constraintEqualToAnchor:myCollectionView.widthAnchor], ]];
Jess Bowers的解决scheme几乎就在那里,但Xcode说UIButton
s没有topAnchor
属性,Jess给出的解决scheme对我不起作用。
对我来说,解决scheme是创build一个与UICollectionView
相同宽度的巨大视图。 看照片。
代码将是这样的:
UIFocusGuide *topButtonFocusGuide = [[UIFocusGuide alloc] init]; topButtonFocusGuide.preferredFocusedView = myButton; [self.view addLayoutGuide:topButtonFocusGuide]; [self.view addConstraints:@[ [topButtonFocusGuide.topAnchor constraintEqualToAnchor:transparentView.topAnchor], [topButtonFocusGuide.bottomAnchor constraintEqualToAnchor:transparentView.bottomAnchor], [topButtonFocusGuide.leadingAnchor constraintEqualToAnchor:transparentView.leadingAnchor], [topButtonFocusGuide.widthAnchor constraintEqualToAnchor:transparentView.widthAnchor], ]];
使用 :
重写弱var preferredFocusedView:UIView? {get {return segmentedControl}}
我遇到的其中一个问题是使用UINavigationController作为登陆页面,当选中某个项目时,将ViewController细节推送到堆栈上。
我在UIButton的canBecomeFocused覆盖内部放置了一个断点,并使用这个快速的小扩展来询问button为什么不可聚焦。
extension UIView { func whyNotFucusable () { print(self.perform(Selector(("_whyIsThisViewNotFocusable"))).takeUnretainedValue()) } }
我不断收到以下错误尝试debugging….
ISSUE: This view may not be focusable because other views or focus guides are occluding it.
首先,我在字典里查了一下,这是什么意思。
verb, formal, technical - stop, close up, or obstruct (an opening, orifice, or passage).
现在的问题是什么观点阻碍了我的UIButton?
我在可视化debugging器中很快打开了UI,并且在我的UIButton上有一个清晰的视图。 检查视图,我发现与 nagivationBar具有默认系统背景色的iOS 不同 ,tvOS的实现具有明确的默认颜色。 我意识到,我没有隐藏导航栏,作为回报,它阻碍了我的button。
我做了以下,并修复了我所有的问题:)
navController.setNavigationBarHidden(true, animated: false)
希望这可以帮助!!!
Swift 3.0
Jess Bower的回答对我来说是巨大的帮助,节省了很多时间。
以下是Swift 3和tvOS 10的工作原理。现在必须使用preferredFocusEnvironments
。 在ViewDidLoad()
self.view.addLayoutGuide(focusGuide) self.focusGuide.topAnchor.constraint(equalTo: self.buttonAbove.topAnchor).isActive = true self.focusGuide.bottomAnchor.constraint(equalTo: self.CollectionView.topAnchor).isActive = true self.focusGuide.leadingAnchor.constraint(equalTo: self.CollectionView.leadingAnchor).isActive = true self.focusGuide.widthAnchor.constraint(equalTo: self.CollectionView.widthAnchor).isActive = true self.focusGuide.preferredFocusEnvironments = [self.buttonAbove]