简体   繁体   English

在UIButton中为不同的文本大小设置图像对齐方式

[英]Set image alignment in UIButton for different text sizes

I have a button in my Navigation bar. 我的导航栏中有一个按钮。 This button is center aligned. 此按钮居中对齐。 When user taps on it a modal view is launched the same way than New section in Apple's Music app for iOS. 当用户点击它时,将以与Apple iOS版音乐应用程序中“ 新建”部分相同的方式启动模式视图。

I want to use this down arrow picker image. 我想使用此向下箭头选择器图像。 I configured the image to be seen on right side (title - image) as per this StackOverflow question . 根据此StackOverflow问题,我将图像配置为在右侧显示(标题图像)。

在此处输入图片说明

在此处输入图片说明

The problem happens when user picks another option from Modal view with a different text size: app updates button text which in some cases overlaps the image. 当用户从“模式”视图中选择另一个具有不同文本大小的选项时,就会发生问题:应用程序更新按钮文本,在某些情况下按钮文本与图像重叠。

在此处输入图片说明

Is it any way to make this image right aligned in all cases, even when the text size changes? 是否可以在所有情况下都使该图像正确对齐,即使文本大小发生变化也可以吗? Thanks 谢谢

Update: I am coding on Swift. 更新:我在Swift上编码。 Swift code snippets would be appreciated. Swift代码片段将不胜感激。

The titleView property of a UINavigationItem does not resizes itself based on its content. UINavigationItemtitleView属性不会根据其内容调整自身大小。 If you ever need the size to change you have to do it yourself. 如果您需要更改尺寸,则必须自己进行更改。

You're best option is to use a custom view that changes its bounds when the content it displays changes to require a different size. 最好的选择是使用一个自定义视图,该视图在其显示的内容更改为需要不同大小时会更改其范围。

The other option I can think of would be to manually adjust the images left inset and the bounds of the button when the text changes. 我可以想到的另一种选择是在文本更改时手动调整图像的左插图和按钮的边界。 This requires you to calculate the width of the text to update the image left inset and the bounds. 这要求您计算文本的宽度以更新图像的左插图和边界。

for creating a custom view which grows based on child views you can do some thing like this: 要创建基于子视图增长的自定义视图,您可以执行以下操作:

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]];

OUTPUT 输出值

一个基于UIButton字符串长度增长的customview

you can take UIImageView in place of UILabel. 您可以使用UIImageView代替UILabel。

I can't figure out how to add this container view in navigation item tite view. 我不知道如何在导航项目的视图视图中添加此容器视图。

This is what final output if you add 这是最终的结果,如果您添加

[self.navigationItem.titleView addSubview:containerView]; 

at last line. 在最后一行。 蒂特视图未居中

may be @clever Error can hep you 可能是@clever错误可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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