简体   繁体   中英

Customization of UIBarButtonItem and interface rotations

There are a lot of talks about customization of UIBarButtonItem with custom view as an UIButton. It's quite clear. What I didn't find so far though, and what surprises me, is that there is no mentioning for custom UIBarButtonItems which handle interface rotations - its a common behavior when you rotate an iPhone, bar buttons get squeezed vertically. However, if you customize UIBarButtonItem in ordinary way (by calling initWithCustomView: method), it will stay non-squeezed after rotating into landscape orientation. Are there any workarounds for that?

好吧,我发现在自定义UIBarButtonItem类中处理UIApplicationDidChangeStatusBarOrientationNotification解决方案相当不错。

I think you should rebuild the UIBarButtonItem (Your Custom UIButton) with new orientation image and text, or reset the frame of custom view.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIButton *rightItemButton = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
    //resize the frame
    if (isLandscape) {

    } else if (isPortrait) {
        ....
    }
}

If you can add the UIBarButtonItem from IB (you don't have to create it programatically) you can set the springs and struts to get your default behaviour. This solution worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM