2016-12-13 56 views
0

(Swift3/iOS10)的右侧覆盖将两(并排)按钮,为的UITextField

我试图把两个按钮(保存/取消)使用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表明子视图的帧大小为0.什么是/惯用法这样做的方法是什么?我觉得我只是在这里扔锤子...

+0

UIView和按钮应该将其translatesAutoresizingMaskIntoConstraints设置为false。我会试验一下,看看你是否可以获得一个带有彩色背景的普通UIView来显示(我似乎记得textField希望这个视图是方形的,并且大小为44x44,但不确定)。 –

回答

1

创建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; 
} 
+0

工作就像一个魅力。我试图努力,我猜。 –