【问题标题】:UIImageView size changesUIImageView 大小变化
【发布时间】:2017-03-01 15:26:08
【问题描述】:

我有带有自定义视图的导航栏。 UIImageView 和 UILabel 是 titleView 的子视图。我将 UITapGestureRecognizer 添加到 titleView 以使其在用户点击它时显示另一个 UIViewController。当您点击 titleView 时,其他 UIViewController 会打开。但是当你在我的titleView 中单击返回按钮时,UIImageView 的大小会发生变化。这是代码

UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:@"#474747"]];
[title setFont:[UIFont fontWithName:@"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;

这里是你可以看到变化的图片

【问题讨论】:

    标签: ios objective-c uiimageview uinavigationbar uitapgesturerecognizer


    【解决方案1】:

    我检查了你的代码将剪辑添加到边界并且不需要两次标题框架。

    _imageView.frame = CGRectMake(0, 0, 30, 30);
    _imageView.layer.cornerRadius = 15.0;
    _imageView.layer.masksToBounds = YES;
    _imageView.clipsToBounds = true;
    

    【讨论】:

      【解决方案2】:

      与其直接设置视图的框架,不如尝试使用Auto Layout;我怀疑发生的事情是您的自定义视图在后退按钮之后重新布局,并且它正在恢复为图像视图的instrinsicSize

      你可以做的是关闭子视图上的translatesAutoresizingMaskIntoConstraints,然后添加约束来定位它们,最后为图像视图添加宽度和高度约束。

      如果您打算国际化您的应用程序,也建议使用自动布局 - 如果您正确设置约束,那么翻译后的标题将适合您,您将不必玩弄硬编码的值。

      【讨论】:

        猜你喜欢
        • 2013-01-14
        • 1970-01-01
        • 1970-01-01
        • 2015-08-29
        • 2012-05-21
        • 2011-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多