1
我試圖讓我的UIBarButtonItem
從右側滑入leftBarButtonItem
的位置。在NavigationItem中從右邊移動UIBarButtonItem
要麼幻燈片放在錯誤的地方,然後跳轉時它被設置爲leftBarButtonItem
或根本不可見。這是我現在正在嘗試的..
func createLeftBarButton() {
// Get the nav bar we'll be working with
var toolbar = self.navigationItem
// Initialise our button
cancelButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
cancelButton.setTitle("+", forState: UIControlState.Normal)
cancelButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
cancelButton.addTarget(self, action: "cancel:", forControlEvents: .TouchUpInside)
cancelButton.titleLabel?.font = UIFont(name: "Courier", size: 34.0)
// Create a placeholder to get the position and size of the leftBarButtonItem
placeholderView = UIView(frame: cancelButton.bounds)
placeholderView.backgroundColor = UIColor.clearColor()
toolbar.leftBarButtonItem = UIBarButtonItem(customView: placeholderView)
// Get the frame and position of the placeholderView
var finalFrame: CGRect = self.view.convertRect(placeholderView.bounds, fromCoordinateSpace: placeholderView)
// Set the frame for the button to the right of the final location.. this is probably wrong at the moment.
cancelButton.frame = CGRectMake(-1 * cancelButton.bounds.size.width, finalFrame.origin.y, cancelButton.bounds.size.width, cancelButton.bounds.size.height)
// Add the button to the view
self.navigationController?.navigationBar.addSubview(cancelButton)
// Animate it to the final position
UIView.animateWithDuration(0.2, animations: {
self.cancelButton.frame = finalFrame
}, completion: { _ in
// Finally set it to the leftBarButtonitem
toolbar.leftBarButtonItem = UIBarButtonItem(customView: self.cancelButton)
})
}
按鈕現在似乎出現在正確的地方,但它並沒有設置動畫 –
這是因爲取消按鈕需要被添加到子視圖。 –
動畫對我來說很好。你什麼時候調用這個函數? – shim