【问题标题】:Set image alignment in UIButton for different text sizes在 UIButton 中为不同的文本大小设置图像对齐
【发布时间】:2016-02-23 05:10:01
【问题描述】:

我的导航栏中有一个按钮。此按钮居中对齐。当用户点击它时,会以与 Apple 的 iOS Music 应用程序中的 New 部分相同的方式启动模式视图。

我想使用这个向下箭头选择器图像。我根据this StackOverflow question 将图像配置为在右侧(标题 - 图像)显示。

当用户从模态视图中选择另一个具有不同文本大小的选项时,就会出现问题:应用更新按钮文本,在某些情况下会与图像重叠。

是否有任何方法可以使该图像在所有情况下都正确对齐,即使文本大小发生变化? 谢谢

更新:我正在使用 Swift 进行编码。 Swift 代码 sn-ps 将不胜感激。

【问题讨论】:

    标签: iphone swift uibutton interface-builder ios9


    【解决方案1】:

    UINavigationItemtitleView 属性不会根据其内容调整自身大小。如果你需要改变尺寸,你必须自己做。

    您最好的选择是使用自定义视图,当它显示的内容更改为需要不同的大小时,该视图会更改其边界。

    我能想到的另一个选项是在文本更改时手动调整左侧插图和按钮的边界。这需要您计算文本的宽度以更新图像的左插图和边界。

    【讨论】:

    • 我同意创建一个自定义视图,该视图根据 chlid 视图调整其边界,但 [self.navigationItem.titleView addSubview:containerView];始终添加到左上角/后退栏按钮项。你能在这里吗?
    • 你想做self.navigationItem.titleView = containerView
    【解决方案2】:

    要创建基于子视图增长的自定义视图,您可以执行以下操作:

    UIView *superview = self.view;
    
    UIView *containerView = [UIView new];
    [containerView setBackgroundColor:[UIColor blackColor]];
    [containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [superview addSubview:containerView];
    //this will be containing my button and my label
    
    UILabel *mylabel = [[UILabel alloc]init];
    [mylabel setBackgroundColor:[UIColor redColor]];
    [mylabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    mylabel.text = @"MyLabel";
    
    UIButton *mybutton = [UIButton
                          buttonWithType:UIButtonTypeRoundedRect];
    [mybutton setTitle:@"My Button ye ye yey yeyeyye yeyey"
              forState:UIControlStateNormal];
    [mybutton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [mybutton setBackgroundColor:[UIColor greenColor]];
    
    [containerView addSubview:mylabel];
    [containerView addSubview:mybutton];
    
    
    NSDictionary * views = NSDictionaryOfVariableBindings(mybutton,mylabel, containerView);
    //create pin of fixed 2 pixes between UIButton and UILabel.Done
    NSArray * horizontalConstraintsforbuttons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-1-[mybutton(<=400)]-2-[mylabel(60)]-1-|" options:0 metrics:nil views:views];
    NSArray * heightConstraintforbutton = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mybutton(==60)]" options:0 metrics:nil views:views];
    NSArray * heightConstraintforLabel = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mylabel(==60)]" options:0 metrics:nil views:views];
    
    [containerView addConstraints:horizontalConstraintsforbuttons];
    [containerView addConstraints:heightConstraintforbutton];
    [containerView addConstraints:heightConstraintforLabel];
    
    
    //container view specific constraints
    
    NSArray *heightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[containerView(==60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)];
    
    [superview addConstraints:heightConstraint];
    
    
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0]];
    
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0]];
    

    输出

    你可以用 UIImageView 代替 UILabel。

    我不知道如何在导航项视图中添加此容器视图。

    如果添加,这就是最终输出

    [self.navigationItem.titleView addSubview:containerView]; 
    

    在最后一行。

    可能是@clever 错误可以帮助你

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2018-12-11
      • 1970-01-01
      • 2017-09-15
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多