在Swift 3中,我編程式地創建了一個帶有由彈性分隔符分隔的自定義按鈕的(底部)工具欄,以分離自定義按鈕,將一個(「前一個」)推到左邊和另一個(「下一個」)視圖的右邊緣。但我一直無法顯示按鈕標題(「上一個」和「下一個」)。請建議我。謝謝。這裏是我的代碼:爲什麼編程方式創建(底部)工具欄按鈕標題沒有顯示?
首先,類聲明和幾個變量:
class TextsController: UIViewController,UIGestureRecognizerDelegate {
let textWebView = WKWebView(frame: .zero)
var toolbar:UIToolbar?
接下來,在viewDidLoad中:
view = textWebView
// Create (bottom) toolbar:
let frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height-44, width: UIScreen.main.bounds.size.width, height: 44)
toolbar = UIToolbar(frame: frame)
toolbar?.sizeToFit()
toolbar?.isHidden = true
self.view.addSubview(toolbar!)
self.toolbar?.isTranslucent = false // Required for setting tintColor & barTintColor below
toolbar?.tintColor = UIColor(red: 0.5, green: 0.0, blue: 1.0, alpha: 1.0) //purple for toolbar items
toolbar?.barTintColor = UIColor.white //white for toolbar color
let triangleLeftButton = UIBarButtonItem(image: UIImage(named: "triangleLeft_15x20"), style: .plain, target: self, action:#selector(showPrevious))
let flexibleSpacer = UIBarButtonItem(barButtonSystemItem:.flexibleSpace , target: self, action: nil)
let triangleRightButton = UIBarButtonItem(image: UIImage(named: "triangleRight_15x20"), style: .plain, target: self, action:#selector(showNext))
triangleLeftButton.title = "Previous"
triangleRightButton.title = "Next"
var items = [UIBarButtonItem]()
items.append(triangleLeftButton)
items.append(flexibleSpacer)
items.append(triangleRightButton)
toolbar?.items = items
在下方有標題想象的圖像,你可能會被混淆了一個UITabBarItem一個的UIBarButtonItem。 – matt
謝謝你的回覆。但我打算使文本(我稱之爲「標題」)動態地改變,這取決於文本目前顯示在WKWebView。例如,如果我當前在第2頁上,我希望triangleLeft按鈕顯示「1」,而triangleRight按鈕顯示「3」(我僅僅使用「Previous」和「Next」作爲我想要解決的臨時佔位符後來)。我稍後將添加功能,使這些按鈕可以將頁面1或頁面3召喚到WKWebView。因此,將所需的單詞作爲自定義圖像的一部分不會有幫助。 – WilliamP
繼續並「打算」。但這不是什麼UIBarButtonItem _is_。現實超越。 – matt