【问题标题】:How do I prevent one UIView from being hidden by another UIView?如何防止一个 UIView 被另一个 UIView 隐藏?
【发布时间】:2019-01-28 23:20:04
【问题描述】:

我正在使用 UIViews 创建一个自定义的、可重复使用的分段控制器,但我遇到了重叠视图的问题。目前看起来是这样的:

您可以看到蓝色选择器位于按钮下方,但我希望它位于 底部且高度为 4 个像素。为此,我有:

let numberOfButtons = CGFloat(buttonTitles.count)
let selectorWidth = frame.width / numberOfButtons
let selectorYPosition = frame.height - 3 <--- This cause it to be hidden behind the button
selector = UIView(frame: CGRect(x: 0, y: selectorYPosition, width: selectorWidth, height: 4))
selector.layer.cornerRadius = 0
selector.backgroundColor = selectorColor
addSubview(selector)
bringSubviewToFront(selector) <--- I thought this would work but it does nothing

这导致选择器 UIView 隐藏在段 UIView 后面(我将 Y 位置设置为 - 3,因此您可以看到它是如何被掩盖的。我实际上希望它是 - 4,但是让它完全消失):

我认为使用bringSubviewToFront() 会将其置于UIView 段的前面,但它似乎没有任何作用。我查看了 Apple View Programming Guide 和许多 SO 线程,但找不到答案。

谁能帮我看看我错过了什么?

完整代码

class CustomSegmentedControl: UIControl {
  var buttons = [UIButton]()
  var selector: UIView!
  var selectedButtonIndex = 0

  var borderWidth: CGFloat = 0 {
    didSet {
      layer.borderWidth = borderWidth
    }
  }

  var borderColor: UIColor = UIColor.black {
    didSet {
      layer.borderColor = borderColor.cgColor
    }
  }

  var separatorBorderColor: UIColor = UIColor.lightGray {
    didSet {

    }
  }

  var commaSeparatedTitles: String = "" {
    didSet {
      updateView()
    }
  }

  var textColor: UIColor = .lightGray {
    didSet {
      updateView()
    }
  }

  var selectorColor: UIColor = .blue {
    didSet {
      updateView()
    }
  }

  var selectorTextColor: UIColor = .black {
    didSet {
      updateView()
    }
  }

  func updateView() {
    buttons.removeAll()
    subviews.forEach { $0.removeFromSuperview() }

    // create buttons
    let buttonTitles = commaSeparatedTitles.components(separatedBy: ",")
    for buttonTitle in buttonTitles {
      let button = UIButton(type: .system)
      button.setTitle(buttonTitle, for: .normal)
      button.setTitleColor(textColor, for: .normal)
      button.backgroundColor = UIColor.white
      button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
      buttons.append(button)
    }

    // make first button selected
    buttons[0].setTitleColor(selectorTextColor, for: .normal)

    let numberOfButtons = CGFloat(buttonTitles.count)
    let selectorWidth = frame.width / numberOfButtons
    let selectorYPosition = frame.height - 3
    selector = UIView(frame: CGRect(x: 0, y: selectorYPosition, width: selectorWidth, height: 4))
    selector.layer.cornerRadius = 0
    selector.backgroundColor = selectorColor
    addSubview(selector)
    bringSubviewToFront(selector)

    let stackView = UIStackView(arrangedSubviews: buttons)
    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .fillEqually
    addSubview(stackView)

    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    stackView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    stackView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
  }

  @objc func buttonTapped(button: UIButton) {
    for (buttonIndex, btn) in buttons.enumerated() {
      btn.setTitleColor(textColor, for: .normal)

      if btn == button {
        let numberOfButtons = CGFloat(buttons.count)
        let selectorStartPosition = frame.width / numberOfButtons * CGFloat(buttonIndex)
        UIView.animate(withDuration: 0.3, animations: { self.selector.frame.origin.x = selectorStartPosition })
        btn.setTitleColor(selectorTextColor, for: .normal)
      }
    }

    sendActions(for: .valueChanged)

  }

}

【问题讨论】:

  • 当你stackView.bringSubviewToFront(selector)时会发生什么?
  • 我刚刚添加了stackView.bringSubviewToFront(selector),但没有任何效果。不过还是谢谢。

标签: swift uiview uisegmentedcontrol


【解决方案1】:

你正在用 stackView 掩盖你的selector

你需要做的:

bringSubviewToFront(selector)

在您添加了所有视图之后。将该行移至updateView() 的底部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多