1

我在導航欄中的右側導航項目上添加了自定義圖像。但是當我旋轉設備的右側按鈕項目堅持肖像位置。導航欄中的UINavigationItem位置在方向上未改變

肖像

enter image description here

景觀

enter image description here

正如你可以看到,從縱向改爲橫向時的菜單圖標貼到原來的位置。

代碼

let menuImage = UIImage(named: "right_menu_icon.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) 


    var right_menu_button = CBarButton.buttonWithType(UIButtonType.Custom) as CBarButton 
    right_menu_button.setBackgroundImage(menuImage, forState: UIControlState.Normal) 
    right_menu_button.setBackgroundImage(UIImage(named:"right_menu_icon_highlight.png"), forState: UIControlState.Highlighted) 
    right_menu_button.addTarget(self, action: "menuButtonPressed", forControlEvents: .TouchUpInside) 
    right_menu_button.frame = CGRectMake(0, 0, menuImage!.size.width, menuImage!.size.height) 
    right_menu_button.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin 


    var rightButton:UIBarButtonItem = UIBarButtonItem(customView: right_menu_button) 
    self.navigationItem.rightBarButtonItem = rightButton 
+0

這解決了我的問題。 right_menu_button.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleHeight –

回答

0

嘗試刪除此代碼:

right_menu_button.autoresizingMask = UIViewAutoresizing.FlexibleleftMargin | UIViewAutoresizing.FlexibleTopMargin 

或將其更改爲:

right_menu_button.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight 
+0

不,這會使按鈕拉伸。 –