【问题标题】:Change Width of UITextfield Input更改 UITextfield 输入的宽度
【发布时间】:2020-01-11 10:00:16
【问题描述】:

有没有办法改变我的 UITextield 的输入大小,使其不与我的“vergessen”-Button 重叠?

Textfield 是一个简单的 TextfieldView,宽度为 315。我添加了“Vergessen?”使用下面的代码以编程方式按钮。

func createForgetButton () {
    let button = UIButton(type: .system)
    button.setTitle("Vergessen?", for: .normal)
    button.addTarget(self, action: #selector(self.vergessenTapped(_:)), for: .touchUpInside)
    button.titleLabel?.font = UIFont(name: "Avenir Next", size: 19.0)
    passwordTextField.rightView = button
    passwordTextField.rightViewMode = .unlessEditing
}

【问题讨论】:

  • 是的,有:),如果你能展示你是如何构建这个视图的,你尝试了什么,什么不起作用,你可能更有可能得到一个好的答案。一切顺利。
  • @Wez 更新了问题。希望有帮助:)
  • 展示如何设置视图的框架或约束。显然,您需要更改文本字段的框架/约束,使其仅上升到按钮的边缘。
  • 白线是整个文本字段。这就是宽度限制

标签: ios swift uibutton uitextfield


【解决方案1】:

对该字段使用以下自定义类。然后更新 IB 或代码中的 .rightPadding 字段以匹配右侧按钮的宽度。

/**
 * Text field with some changes according to design
 *
 * - author: Alexander Volkov
 * - version: 1.0
 */
@IBDesignable public class CustomTextField: UITextField {

    /// the left padding
    @IBInspectable public var leftPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }

    /// the right padding
    @IBInspectable public var rightPadding: CGFloat = 0 { didSet { self.setNeedsLayout() } }

    /// Text rectangle
    ///
    /// - Parameter bounds: the bounds
    /// - Returns: the rectangle
    override public func textRect(forBounds bounds: CGRect) -> CGRect {
        let originalRect: CGRect = super.editingRect(forBounds: bounds)
        return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
    }

    /// Editing rectangle
    ///
    /// - Parameter bounds: the bounds
    /// - Returns: the rectangle
    override public func editingRect(forBounds bounds: CGRect) -> CGRect {
        let originalRect: CGRect = super.editingRect(forBounds: bounds)
        return CGRect(x: originalRect.origin.x + leftPadding, y: originalRect.origin.y, width: originalRect.size.width - leftPadding - rightPadding, height: originalRect.size.height)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多