【问题标题】:Strange black bar in middle of UITextField in landscape mode横向模式下 UITextField 中间出现奇怪的黑条
【发布时间】:2020-05-10 10:53:13
【问题描述】:

我尝试通过视图调试器对其进行调试,但仍然无法检测到原因。

如果可能有一些隐藏的问题,TextField 具有以下自定义类

class ChatTextField: UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initNib()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initNib()
    }

    func initNib() {

        self.attributedPlaceholder = NSAttributedString(string: "Send a message to Kael",
                                                        attributes: [NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.1879999936, green: 0.1979999989, blue: 0.2930000126, alpha: 1)])
        self.layer.borderWidth = 2
        self.layer.cornerRadius = 4.0
        self.layer.borderColor = #colorLiteral(red: 0.3059999943, green: 0.5329999924, blue: 0.5490000248, alpha: 1)

        self.addInnerShadow(onSide: .all, shadowColor: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2), shadowSize: 10, cornerRadius: 15, shadowOpacity: 0.8)
    }
    let padding = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 30)

     override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
     }

     override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
     }

     override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
     }
}

【问题讨论】:

  • 你能分享一下这个屏幕的 UI 或 Storyboard 吗?
  • @HardikS 这就是你要的?
  • 为什么你在initNib()中写了这行addInnerShadow
  • 也许您的内部阴影代码导致UITextField 出现黑条?
  • 很高兴为您提供帮助。我已经写下了我的答案。您可以将其标记为已接受并已投票:)

标签: ios swift xcode uikit swift5


【解决方案1】:

您正在调用一个方法来从initNib()UITextField 扩展的方法中提供内阴影。

您将不得不根据纵向和横向模式设置不同的offset 来修改阴影方法。

尝试删除将阴影应用到文本字段的阴影方法,然后检查它是否正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2010-10-14
    • 2012-12-14
    相关资源
    最近更新 更多