UIBarButtonItem具有单独的纵向和横向图像-从UINavigationController弹出视图控制器时未调用layoutSubviews [英] UIBarButtonItem with separate portrait and landscape images - layoutSubviews not called when popping a view controller from UINavigationController

查看:43
本文介绍了UIBarButtonItem具有单独的纵向和横向图像-从UINavigationController弹出视图控制器时未调用layoutSubviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UINavigationController的UIToolbar中显示完全自定义的按钮,并支持纵向和横向.目前,我已经实现了RotatingButton(一个UIView子类)类,其中包含一个UIButton,它填充了整个RotatingButton框架.RotatingButton还包含两个图像,分别用于纵向和横向,并且这些图像的高度不同.然后,将此RotatingButton打包到UIBarButtonItem中作为自定义视图.

I want to show completely custom buttons in UINavigationController's UIToolbar, and support portrait and landscape. Currently I have implemented a RotatingButton (a UIView subclass) class, which contains one UIButton that fills the whole RotatingButton frame. A RotatingButton also contains two images, for portrait and landscape orientations, and the heights of these images differ. Then this RotatingButton gets wrapped into UIBarButtonItem as a custom view.

当前,在RotatingButton的layoutSubviews中,我设置整个视图的边界,并将按钮设置为适合当前方向的图像.这样效果很好,并可以根据需要处理旋转.

Currently, in RotatingButton's layoutSubviews, I am setting the whole view's bounds, and setting the button the appropriate image for the current orientation. This works well and handles rotations as desired.

- (void) createLayout {
    [self addButtonIfNeeded];
    UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    if(UIInterfaceOrientationIsLandscape(currentOrientation)) {
        [self.button setImage:self.landscapeImage forState:UIControlStateNormal];
        self.button.frame = CGRectMake(0.0, 0.0, self.landscapeImage.size.width / 2, self.landscapeImage.size.height / 2);
        self.bounds = CGRectMake(0.0, 0.0, self.landscapeImage.size.width / 2, self.landscapeImage.size.height / 2);
    } else {
        [self.button setImage:self.portraitImage forState:UIControlStateNormal];
        self.button.frame = CGRectMake(0.0, 0.0, self.portraitImage.size.width / 2, self.portraitImage.size.height / 2);
        self.bounds = CGRectMake(0.0, 0.0, self.portraitImage.size.width / 2, self.portraitImage.size.height / 2);
    }
}

- (void) layoutSubviews {
    [super layoutSubviews];
    [self createLayout];
}

但是,此问题仍然存在:

However, this problem remains:

  1. 从纵向开始查看
  2. 将视图控制器推入堆栈
  3. 旋转设备使其横向移动(当前视图会做出适当反应)
  4. 弹出最后一个视图控制器:上一个视图的反应很好,但是RotatingButtons的layoutSubviews没有被调用,并且按钮的大小超出了应有的大小.

因此,当前,在弹出视图控制器后,以前的UIBarButtonItems不会调用其layoutSubviews,并且它们仍然太大(或者太小,如果我们从横向开始并在另一个视图中旋转为纵向).如何解决这个问题?

So, currently after popping a view controller, the previous UIBarButtonItems don't have their layoutSubviews called, and they remain too large (or too small, if we start from landscape and rotate to portrait in another view). How to solve this problem?

推荐答案

我没有找到一个完全令人满意的解决方案,但是我的按钮恰巧大小合适,这种解决方案对我来说非常有效:

I didn't find a completely satisfactory solution, but my buttons happened to be of suitable size, and this kind of a solution worked very well for me:

UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:target action:selector];
UIImage *barButton = [portraitImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
UIImage *barButton_land = [landscapeImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[b setBackgroundImage:barButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[b setBackgroundImage:barButton_land forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];

然后显然将创建的按钮添加为rightBarButtonItem/leftBarButtonItem,或者您可能想使用它.

And then obviously adding the created button as rightBarButtonItem/leftBarButtonItem, or as you may want to use it.

此问题是,如果您的按钮不够宽,则按钮可能看起来完全错误(因为在此解决方案中平铺了图像的中间内容).

The problem with this is that if your buttons are not wide enough, the buttons may look completely wrong (since the middle content of the image is tiled in this solution).

这篇关于UIBarButtonItem具有单独的纵向和横向图像-从UINavigationController弹出视图控制器时未调用layoutSubviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆