UITextField RightView大小不正确

时间:2018-08-14 09:50:55

标签: ios swift uikit sizing

问题 enter image description here

当将rightView放在UITextField内的UICollectionView单元中时,UIStackView的大小没有正确调整

代码:

  func configure(cell: UICollectionViewCell) {
    guard let cell = cell as? ListTextFieldCell else {return}
    self.cell = cell
    cell.title.text = "PIN"
    let textField = cell.textField!
    textField.rightViewMode = .always
    updateButtonTitle()
    textField.rightView = revealPasswordButton
    textField.placeholder = "4 digits"
    textField.keyboardType = .numberPad
    textField.delegate = self
    updateButtonTitle()
    // No effect!
    revealPasswordButton.frame = CGRect(origin: .zero, size: CGSize(width: 100, height: 50))
  }


    // no effect WHATSOEVER
    revealPasswordButton.setTitle(buttonTitle, for: .normal)
    revealPasswordButton.setContentCompressionResistancePriority(.required, for: .horizontal)
    revealPasswordButton.setContentCompressionResistancePriority(.required, for: .vertical)
    revealPasswordButton.sizeToFit()
  }

另一方面,在普通的UIView中,完全相同的代码会产生正确的结果:

enter image description here

这种问题可能是什么情况

1 个答案:

答案 0 :(得分:0)

您的UIStackView分发设置是什么?另外,您可能希望将contentCompressionResistancePriority

UILayoutPriorityDefaultRequired水平值设置为rightView