如何使用分段控制在 iphone 上添加超过 6 个分段和图像
Posted
技术标签:
【中文标题】如何使用分段控制在 iphone 上添加超过 6 个分段和图像【英文标题】:How to add more than 6 segments with image on iphone using uisegmentcontrol 【发布时间】:2014-11-19 12:15:19 【问题描述】:问题是,如果我使用文本,我可以添加任意多的片段,但如果我使用图像,则意味着我无法在 iphone 上添加超过 6 个片段。
帮帮我。
UIImage* resetImage = [UIImage imageNamed:@"Reset_V1"];
UIImage* wlwwImage = [UIImage imageNamed:@"Reset_V1"];
UIImage* huImage = [UIImage imageNamed:@"HU_PI_V1"];
UIImage* panImage = [UIImage imageNamed:@"Panning_V1"];
UIImage* disImage = [UIImage imageNamed:@"Distance_V1"];
UIImage* areaImage = [UIImage imageNamed:@"Reset_V1"];
UIImage* clrImage = [UIImage imageNamed:@"Clear_V1"];
UIImage* presetImage = [UIImage imageNamed:@"Presets_V1"];
mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage, nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[segmentControl addTarget:self action:@selector(whichSegment:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
【问题讨论】:
嗨,我提交了答案,演示项目是帮助还是还需要更多? 【参考方案1】:我尝试使用相同的代码,它可以工作输出:
代码:
UIImage* resetImage = [UIImage imageNamed:@"Image"];
UIImage* wlwwImage = [UIImage imageNamed:@"Image"];
UIImage* huImage = [UIImage imageNamed:@"Image"];
UIImage* panImage = [UIImage imageNamed:@"Image"];
UIImage* disImage = [UIImage imageNamed:@"Image"];
UIImage* areaImage = [UIImage imageNamed:@"Image"];
UIImage* clrImage = [UIImage imageNamed:@"Image"];
UIImage* presetImage = [UIImage imageNamed:@"Image"];
UIImage* presetImage1 = [UIImage imageNamed:@"Image"];
UIImage* presetImage2= [UIImage imageNamed:@"Image"];
UIImage* presetImage3= [UIImage imageNamed:@"Image"];
NSArray *mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage,presetImage1,presetImage2,presetImage3, nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[self.view addSubview:segmentControl];
下载演示项目Link
如果您的图片路径无效,那么它将仅显示正确图片的片段
代码(有错误)
// Do any additional setup after loading the view, typically from a nib.
UIImage* resetImage = [UIImage imageNamed:@"Image"];
UIImage* wlwwImage = [UIImage imageNamed:@"Image"];
UIImage* huImage = [UIImage imageNamed:@"Image"];
UIImage* panImage = [UIImage imageNamed:@"Image"];
UIImage* disImage = [UIImage imageNamed:@"Image"];
UIImage* areaImage = [UIImage imageNamed:@"Image1"]; // wrong image name
UIImage* clrImage = [UIImage imageNamed:@"Image1"]; // wrong image name
UIImage* presetImage = [UIImage imageNamed:@"Image1"]; // wrong image name
UIImage* presetImage1 = [UIImage imageNamed:@"Image1"]; // wrong image name
UIImage* presetImage2= [UIImage imageNamed:@"Image1"]; // wrong image name
UIImage* presetImage3= [UIImage imageNamed:@"Image1"]; // wrong image name
NSArray *mySegments = [[NSArray alloc] initWithObjects: wlwwImage,panImage,huImage,disImage,areaImage,resetImage,clrImage,presetImage,presetImage1,presetImage2,presetImage3, nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:mySegments];
CGRect myFrame = CGRectMake(0, 65, 320, 35);
segmentControl.frame = myFrame;
[self.view addSubview:segmentControl];
【讨论】:
以上是关于如何使用分段控制在 iphone 上添加超过 6 个分段和图像的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 SwiftUI 在 MacOS 上配置多选分段控制