在 NavigationItem 中从右侧动画化 UIBarButtonItem
Animate UIBarButtonItem from right in NavigationItem
我正在尝试让我的 UIBarButtonItem
从右侧滑入 leftBarButtonItem
位置。
要么滑动到错误的位置然后在设置为 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)
})
}
您使用占位符视图/子视图的方法有点过于复杂。实际上,您的代码根本不显示任何内容,因为您的 cancelButton 没有框架,然后您的占位符视图继承了该空框架。但即便如此,您仍然会遇到按钮滑动到应有位置下方然后弹回的问题。
认为这可以满足您的需求:
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)
cancelButton.frame = CGRectMake(0, 0, 20, 20)
// Create a placeholder to get the position and size of the leftBarButtonItem
toolbar.leftBarButtonItem = UIBarButtonItem(customView: cancelButton)
var delta = self.cancelButton.frame.size.width
cancelButton.frame = CGRectOffset(cancelButton.frame, -delta, 0.0)
// Animate it to the final position
UIView.animateWithDuration(0.2, animations: {
self.cancelButton.frame = CGRectOffset(self.cancelButton.frame, delta, 0.0)
}, completion: { _ in
})
}
我正在尝试让我的 UIBarButtonItem
从右侧滑入 leftBarButtonItem
位置。
要么滑动到错误的位置然后在设置为 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)
})
}
您使用占位符视图/子视图的方法有点过于复杂。实际上,您的代码根本不显示任何内容,因为您的 cancelButton 没有框架,然后您的占位符视图继承了该空框架。但即便如此,您仍然会遇到按钮滑动到应有位置下方然后弹回的问题。
认为这可以满足您的需求:
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)
cancelButton.frame = CGRectMake(0, 0, 20, 20)
// Create a placeholder to get the position and size of the leftBarButtonItem
toolbar.leftBarButtonItem = UIBarButtonItem(customView: cancelButton)
var delta = self.cancelButton.frame.size.width
cancelButton.frame = CGRectOffset(cancelButton.frame, -delta, 0.0)
// Animate it to the final position
UIView.animateWithDuration(0.2, animations: {
self.cancelButton.frame = CGRectOffset(self.cancelButton.frame, delta, 0.0)
}, completion: { _ in
})
}