2015-04-28 48 views
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) 
    }) 


} 

回答

1

您的佔位符視圖/子視圖的方法有點過於複雜。因爲你的代碼根本不顯示任何東西,因爲你的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 
    }) 
} 
+0

按鈕現在似乎出現在正確的地方,但它並沒有設置動畫 –

+0

這是因爲取消按鈕需要被添加到子視圖。 –

+0

動畫對我來說很好。你什麼時候調用這個函數? – shim