ios - 将两个(并排)按钮作为 UITextField 的右侧覆盖

标签 ios swift uitextfield uikit overlay

(Swift3/iOS10)

我正在尝试使用 rightView 属性在 UITextField 中放置两个按钮(保存/取消)。我的基本设置代码(从 viewDidLoad 调用)如下:

private func accesorizeRenameField() {
    let saveButton = UIButton(type: .system)
    saveButton.setImage(#imageLiteral(resourceName: "buttonCheckmark"), for: .normal)
    saveButton.addTarget(self, action: #selector(renameSave), for: .touchUpInside)
    saveButton.tintColor = UIColor(white: 0.15, alpha: 1)

    let cancelButton = UIButton(type: .system)
    cancelButton.setImage(#imageLiteral(resourceName: "buttonX"), for: .normal)
    cancelButton.addTarget(self, action: #selector(renameCancel), for: .touchUpInside)
    cancelButton.tintColor = UIColor(227, 34, 60, 1)

    let container = UIView()
    container.addSubview(saveButton)
    container.addSubview(cancelButton)
    container.translatesAutoresizingMaskIntoConstraints = false
    container.backgroundColor = UIColor.cyan

    saveButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
    cancelButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
    saveButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
    cancelButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
    saveButton.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
    saveButton.trailingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
    cancelButton.leadingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
    cancelButton.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true

    self.renameField.rightView = container
    self.renameField.rightViewMode = .always
}

稍后,当我通过一些动画公开重命名字段时,我调用

self.renameField.rightView?.bounds = CGRect(x: 0, y: 0, width: self.renameField.bounds.height * 2, height: self.renameField.bounds.height)
self.renameField.rightView?.setNeedsLayout()
"rightView.frame \(self.renameField.rightView?.frame)".print()
self.renameField.rightView?.subviews.enumerated().forEach() {index,child in
    "child \(index) frame \(child.frame)".print()
}

但是,我无法让按钮显示出来。出现了青色背景(用于调试),但实际上是正方形的(应该是 2:1 的矩形)。 print 显示 subview 的帧大小为 0。执行此操作的正确/惯用 方法是什么?我觉得我只是在这里扔锤子......

最佳答案

将 UIButton 创建为 .custom 类型而不是 .system。

并且因为您已经知道 viewDidLoad 中 renameField 的高度,所以您不需要为约束而烦恼。

下面的代码就足够了。

override func
viewDidLoad() {
    super.viewDidLoad()

    let wSize = renameField.frame.size.height - 2

    let saveButton = UIButton( type: .custom )
    saveButton.setImage( #imageLiteral(resourceName: "buttonCheckmark"), for: .normal )
    saveButton.addTarget( self, action: #selector(renameSave), for: .touchUpInside )
    saveButton.frame = CGRect( x: 0, y: 0, width: wSize, height: wSize )

    let cancelButton = UIButton( type: .custom )
    cancelButton.setImage( #imageLiteral(resourceName: "buttonX"), for: .normal )
    cancelButton.addTarget( self, action: #selector(renameCancel), for: .touchUpInside )
    cancelButton.frame = CGRect( x: wSize, y: 0, width: wSize, height: wSize )

    let wV = UIView()
    wV.frame = CGRect( x:0, y:0, width: wSize * 2, height: wSize )
    wV.addSubview( saveButton )
    wV.addSubview( cancelButton )

    renameField.rightView = wV;
    renameField.rightViewMode = .always;
}

关于ios - 将两个(并排)按钮作为 UITextField 的右侧覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130828/

相关文章:

ios - iOS 中每个国家/地区的 IAP 定价不同

ios - sqlite3_step 总是返回 SQLITE_DONE

ios - 更智能的自动资本化

ios - UITextView/UITextField 的虚线/虚线边框

ios - 当滚动出 View 时,覆盖的 setSelected 和 setHighlighted 不保留选定的背景

ios - 如何以编程方式在 View Controller 之间传递数据?我想将 UITextfield 数据传递给另一个 View Controller

ios - 如何从 UIImagePickerController 获取 PNG 表示形式?

iOS UICircularProgressRing 径向渐变

ios - Objective-C 和 Swift URL 编码

Swift:动态库和 AFNetworking