【问题标题】:UICollectionViewCell to UIButton Focus in tvOStvOS 中的 UICollectionViewCell 到 UIButton 焦点
【发布时间】:2015-10-20 15:00:10
【问题描述】:

我有一个包含 12-13 个 UICollectionViewCells 的 UICollectionView。我可以轻松地专注于 UICollectionViewCells 并且一切正常。 UICollectionView 外面有一个 UIButton。如果我在第一个单元格或第二个单元格上并且向上滑动,那么我可以轻松地专注于 UIButton。当我在第三个单元格上时,我无法将焦点移动到 UIButton。有什么想法吗?

【问题讨论】:

    标签: ios tvos apple-tv


    【解决方案1】:

    您应该使用包含您想要聚焦的 UIButton 的 UIFocusGuide。使 UIFocusGuide 与 collectionView 一样宽,并且足够高以覆盖按钮。将preferredFocusView设为按钮。

      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 - 这让我发疯了。请注意,如果您在使用它时遇到问题,则可能是因为您的一个锚点嵌入在另一个视图中。我在滚动视图中有一个集合视图,并且使用集合视图底部作为我的顶部锚点不起作用。将其更改为滚动视图底部有效。
    • 像魅力一样工作!谢谢。
    • 嗨,杰西,我还有一个问题。如果我在右侧而不是左侧有按钮,并且如果我只有一个行集合视图,我将无法使集合视图成为焦点。有什么解决办法吗?
    • @Vijay_07 这可能值得提出一个新的堆栈问题。不确定。
    【解决方案2】:

    Jess Bowers 解决方案几乎就在那里,但 Xcode 说 UIButtons 没有 topAnchor 属性,并且 Jess 提供的解决方案对我不起作用。

    对我来说,解决方案是创建一个与顶部的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],
      ]];
    

    【讨论】:

    • UIButton 是 UIControl 的子类,它是 UIView 的子类。 developer.apple.com/library/tvos/documentation/UIKit/Reference/… 另请查看在按钮上使用 topAnchor 的 Apple 示例。 developer.apple.com/library/prerelease/tvos/samplecode/…
    • 好的,但是 Xcode 说按钮没有 topAnchor,不幸的是 Jess 的解决方案对我不起作用。
    • 还有其他问题。我刚刚输入了我的按钮名称,输入了 .top 并点击了转义,自动完成显示我 topAnchor。当前的 Xcode。此外,Jess 的解决方案也不适合我。我在断点处检查指南的框架,它看起来正确。但是没有效果。
    • 这很奇怪。无论如何,它对我不起作用。
    • 是的,这很奇怪。我已经多次使用过这种技术。 UIButton 肯定有一个 topAnchor。设置可能有一些特定和不同的地方。在问题中发布更多代码可能会有所帮助,我很乐意提供反馈。我所做的某些假设可能不适用于您的情况。 UIFocusGuide 和布局指南旨在消除您对虚拟视图的需求。但它们确实有效。
    【解决方案3】:

    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]
    

    【讨论】:

      【解决方案4】:

      我面临的一个问题涉及使用 UINavigationController 登录页面,以便在选择项目时将详细视图控制器推送到堆栈中。

      我在 UIButton 的 canBecomeFocused 覆盖中放置了一个断点,并使用这个快速的小扩展询问按钮为什么它不可聚焦。

      extension UIView {
          func whyNotFucusable () {
              print(self.perform(Selector(("_whyIsThisViewNotFocusable"))).takeUnretainedValue())
          }
      }
      

      我在尝试调试时不断收到以下错误....

      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???

      我在可视化调试器中快速打开 UI,在我的 UIButtons 顶部有一个清晰的视图。检查视图我发现 不像 iOS,其中 nagivationBar 具有默认的系统背景颜色,tvOS 的实现具有清晰的默认颜色。我意识到我没有隐藏导航栏,作为回报,它挡住了我的按钮。

      我做了以下,它解决了我所有的问题:)

      navController.setNavigationBarHidden(true, animated: false)
      

      希望对你有帮助!!!

      【讨论】:

        【解决方案5】:

        使用:

        覆盖弱变量 PreferredFocusedView:UIView? { 得到 { 返回分段控制 } }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-21
          • 2015-11-18
          • 2016-09-12
          • 1970-01-01
          • 1970-01-01
          • 2016-01-03
          相关资源
          最近更新 更多