简体   繁体   English

无法在细分之间进行UISegment Control的切换

[英]Unable to switch between segments for UISegment Control

Below is the code snippet I am using to set up my UISegment Control 以下是我用来设置UISegment控件的代码段

//Add UIView below the nav bar
UIView *buttonContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];

[self.view addSubview:buttonContainer];

//Set up segment control
NSString *nicknameLabel = [NSString stringWithFormat:@"%@",self.nickname];

UISegmentedControl *tempSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:nicknameLabel,@"Friends", @"Everyone", nil]];
tempSegmentControl.frame = CGRectMake(-8, 44, 336, 30);

self.segmentControl = tempSegmentControl;
[self.segmentControl setWidth:112 forSegmentAtIndex:0];
[self.segmentControl setWidth:112 forSegmentAtIndex:1];
[self.segmentControl setWidth:112 forSegmentAtIndex:2];
self.segmentControl.selectedSegmentIndex = 0;
[self.segmentControl addTarget:self action:@selector(toggleControls:) forControlEvents:UIControlEventValueChanged];
[self.segmentControl setSegmentedControlStyle:UISegmentedControlStylePlain];

[self changeSegmentFontSize];

[tempSegmentControl release];
[buttonContainer addSubview:self.segmentControl];
[self.view bringSubviewToFront:buttonContainer];
[buttonContainer bringSubviewToFront:self.segmentControl];
[buttonContainer release];

However, I in the app, I am unable to switch between the segments for the segmented control. 但是,在应用程序中,我无法在分段控件的分段之间进行切换。 (only show default first segment highlighted) (仅显示默认的第一段突出显示)

在此处输入图片说明

Any advice on how I can fix this? 关于如何解决此问题的任何建议?

It looks like a bounds problem. 它看起来像一个边界问题。 The containing view has bounds {0, 0, 320, 30} while the segmented control has {-8, 44, 336, 30}. 包含视图的边界为{0,0,320,30},而分段控件的边界为{-8,44,336,30}。 This means the segmented control is technically "outside" its containing view and the iOS hit test will not register a touch. 这意味着分段控件在技术上在其包含视图的“外部”,并且iOS命中测试不会注册触摸。

Try not setting the segmented control's frame (temporarily comment out the tempSegmentControl.frame = CGRectMake(-8, 44, 336, 30); line) and see what happens. 尝试不要设置分段控件的框架(暂时注释tempSegmentControl.frame = CGRectMake(-8, 44, 336, 30);行),然后看看会发生什么。

  1. Check that userInteractionEnabled is set to TRUE for your view, buttonContainer, and segment control. 检查您的视图,buttonContainer和段控件的userInteractionEnabled是否设置为TRUE。

  2. Check that the bounds of the buttonContainer's superview are wide and tall enough to fully contain the buttonContainer. 检查buttonContainer的超级视图的边界足够宽和足够高以完全包含buttonContainer。 If they're not then hit-testing wont work and the segmented control wont get any touches. 如果不是这样,那么命中测试将无法正常工作,而分段控件将无法获得任何效果。

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

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