【发布时间】:2013-07-15 01:44:15
【问题描述】:
当我在手机上玩游戏时,我注意到我的 UISegmentedControl 响应不快。让我的水龙头注册需要 2 次或更多次尝试。所以我决定在模拟器中运行我的应用程序,以更准确地探测问题所在。通过用鼠标单击数十次,我确定 UISegmentedControl 的前 25% 没有响应(在下面的屏幕截图中,该部分用 Photoshop 以红色突出显示)。我不知道有任何不可见的 UIView 可能会阻止它。你知道如何让整个控件可以点击吗?
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Uno", @"Dos", nil]];
self.segmentedControl.selectedSegmentIndex = 0;
[self.segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
self.segmentedControl.height = 32.0;
self.segmentedControl.width = 310.0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.tintColor = [UIColor colorWithWhite:0.9 alpha:1.0];
self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
UIView* toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HEADER_HEIGHT)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(
toolbar.bounds.origin.x,
toolbar.bounds.origin.y,
// * 2 for enough slack when iPad rotates
toolbar.bounds.size.width * 2,
toolbar.bounds.size.height
);
gradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor
colorWithWhite:0.8
alpha:1.0
] CGColor
],
nil
];
[toolbar.layer insertSublayer:gradient atIndex:0];
toolbar.backgroundColor = [UIColor navigationBarShadowColor];
[toolbar addSubview:self.segmentedControl];
UIView* border = [[UIView alloc] initWithFrame:CGRectMake(0, HEADER_HEIGHT - 1, toolbar.width, 1)];
border.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
border.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0];
border.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[toolbar addSubview:border];
[self.segmentedControl centerInParent];
self.tableView.tableHeaderView = toolbar;
【问题讨论】:
-
HEADER_HEIGHT 是什么值(假设它是宏或常量)?这段代码是从哪里调用的 - viewDidLoad?
-
HEADER_HEIGHT = 42.0;代码在viewDidLoad[super viewDidLoad]之后调用。我使用UIView+Position类别,它具有方便的属性,例如 x、y、宽度、高度和 centerInParent。 -
这可能有助于stackoverflow.com/a/9719364/488611。导航栏触摸区域在下方延伸,以便于点击。
-
你试过打开模拟器选项
Debug -> Color blended layers吗?我也许可以给你看一些你不知道的覆盖物,它们会阻止你触摸 -
如果有任何安慰:您在 Apple 自己的应用程序中遇到相同的行为,当他们使用导航栏下方的分段控件时。
标签: ios cocoa-touch uisegmentedcontrol