2016-12-13 55 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

工作就像一個魅力。我試圖努力,我猜。 –