尽我所能,我无法解决iOS7 iPhone应用程序的UISegmentedControl错误。
当我创建分段控件时,我使用以下代码:
NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil];
_wheelDiameterSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedControlItemArray];
_wheelDiameterSegmentedControl.frame = CGRectMake(0, 102, 290, 50);
_wheelDiameterSegmentedControl.selectedSegmentIndex = 0;
_wheelDiameterSegmentedControl.tintColor = [UIColor colorWithRed:0.35 green:0.4 blue:0.9 alpha:1.0];
for (id segment in [_wheelDiameterSegmentedControl subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
titleLabel.numberOfLines = 0;
}
}
}
[_wheelDiameterSegmentedControl addTarget:self
action:@selector(pickOne:)
forControlEvents:UIControlEventValueChanged];
[_wheelDiameterMenuContainer addSubview:_wheelDiameterSegmentedControl];
可悲的是,我无法发布图片,或者我会向您展示我想要的控件:UISegmented Control中的每个段都有两行文本,换行符正是我要求它的地方
但是,在旋转时,我想保持分段控件的全宽,并且换行在宽段中看起来很傻。所以,在willAnimateRotationToInterfaceOrientation中,我已经包含了以下代码,字符串中没有换行符:
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 450, 50)];
[_wheelDiameterSegmentedControl setTitle:@"Nominal: 27 inch" forSegmentAtIndex:0];
[_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 23mm" forSegmentAtIndex:1];
[_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 20mm" forSegmentAtIndex:2];
再次,如果我可以插入图像,我会向您显示我想要的图像:一个宽的UISegmented控件,标签中没有换行符(每个标签1行文本)。
这是我遇到麻烦的地方。当我旋转回肖像时,我的选择似乎是:
1行标签文本,截断,格式为
“实际:7 ......”
当我只使用
重置UISegmentedControl的大小时[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)];
2行标签文本,截断,格式为
“实际:
700c x ......“
当我重置大小并重置字符串值并重新运行将标签的numberOfLines设置为2的代码循环时,使用
NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil];
[_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:0] forSegmentAtIndex:0];
[_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:1] forSegmentAtIndex:1];
[_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:2] forSegmentAtIndex:2];
for (id segment in [_wheelDiameterSegmentedControl subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
titleLabel.numberOfLines = 2;
}
}
}
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)];
3行标签文字,格式为
“实际:
700c x
20毫米“
这是我在上面使用numberOfLines = 0替换上面的强制numberOfLines = 2时得到的,当我在第一时间设置UISegmentedControl时这是有效的。
我想要的是我在创建控件时获得的内容,即
“实际:
700c x 20mm“
但无论我尝试过什么(将字符串和numberOfLines代码放入willRotateToInterfaceOrientation或didRotateFromInterfaceOrientation;在更改文本之前重新设置UISegmentedControl的框架;在更改文本后重新设置框架...),我不能得到我漂亮,整洁,双线的标签。我在这里错过了什么?
答案 0 :(得分:8)
啊哈!操作系统在旋转时如何处理标签大小的方法与标签在创建时的处理方式不同。所以我使用
强制标签框架的大小for (id segment in [_wheelDiameterSegmentedControl subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
//inserting line here, to make the frame behave nicely:
//
titleLabel.frame = CGRectMake(0, 0, 97, 50);
//
//continuing as before
titleLabel.numberOfLines = 0;
}
}
}
现在它完全按我想要/期望显示。好哇!
如果您只想在UISegmentedControl中获取两行(或更多行)文本。使用Herzian的惊人技术!这是另一个代码示例,可以帮助人们......
self.yourSlider .. set the height manually (can't easily do it in XIB)
// now use amazing Herzian technology...
for (id segment in [self.yourSlider subviews])
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
UILabel *titleLabel = (UILabel *) label;
titleLabel .. set the width to say 80, per your layout;
(the width us typically too low)
titleLabel.numberOfLines = 0;
}
}
再次非常感谢你。
答案 1 :(得分:1)
ios 7中的工作代码:
我有一个包含四个段的段控制器,我需要它在2行中用于文本。所以我在UILabel
的片段中添加了subview
作为UISegmentedControl
并且它运作良好。我已经尝试了所有其他方法,但在ios 7和ios 8中它现在运行良好。
for(int k =0;k<4;k++)
{
NSString *text = @"title name";
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(10,2, (self.segmentcontroll.frame.size.width/4), 34);
label.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
label.textColor = [UIColor whiteColor];
label.text = text;
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];
[[[self.segmentcontroll subviews] objectAtIndex:k] addSubview:label];
}
答案 2 :(得分:0)
Herzian的解决方案作为一个类别,如果有人想要它。请记住在VWA中调用它而不是VDL,因此它会在自动调整/旋转后发生
#import "UISegmentedControl+MultiLine.h"
@implementation UISegmentedControl (MultiLine)
-(void)setupMultiLine
{
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height*1.6);
for (id segment in self.subviews)
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
UILabel *titleLabel = (UILabel*) label;
titleLabel.frame = CGRectMake(0,
0,
titleLabel.frame.size.width,
titleLabel.frame.size.height*1.6);
titleLabel.numberOfLines = 0;
}
}
}
}
@end