uisegmentedcontrol不检测某些段上的触摸

时间:2011-11-07 19:46:08

标签: iphone uiscrollview touch uisegmentedcontrol

我在水平滚动的uiscrollview中有一个uisegmentedcontrol。 scrollview工作得很好,内容根据segmentedcontrol的宽度调整大小。分段控件检测首次显示时可见的那些段上的触摸。如果我向右滚动,则无法选择新显示的段。 cuttof也不是一个片段,它最初显示的是什么,就像有一个视图剪切它。感谢您的帮助,我已从滚动视图中取消选择剪辑子视图。

// creates segmented control to indicate test to show on graph

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:self.labels];
    segmentedControl.frame = CGRectMake(15, 41, 285, 30);
    segmentedControl.autoresizingMask = 
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [segmentedControl addTarget:self action:@selector(testChange:) forControlEvents:UIControlEventValueChanged];

    //checks the length of the text in each segment and asjusts control accordingly
    int x = 0;
    segmentWidth = 0.0;
    for (id segment in [segmentedControl subviews]) 
    {
        for (id label in [segment subviews]) 
        {
            if ([label isKindOfClass:[UILabel class]])
            {

                [label setTextAlignment:UITextAlignmentCenter];
                [label setFont:[UIFont boldSystemFontOfSize:12]];

                CGFloat textWidth = [[segmentedControl titleForSegmentAtIndex:x] sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 20;
                [segmentedControl setWidth:textWidth forSegmentAtIndex:x];
                segmentWidth += textWidth;
            }
        }
        x++;
    }

    self.testSegment = segmentedControl;
    [segmentedControl release];

    //adds segmentedcontroll to scrollview, and adjusts contensize
    [scrollView setContentSize:CGSizeMake(segmentWidth + 45, scrollView.frame.size.height)];
    [scrollView showsHorizontalScrollIndicator];
    [scrollView addSubview:testSegment];

1 个答案:

答案 0 :(得分:1)

修复了,问题是我在计算每个段的宽度之前设置了分段控件的帧。

segmentedControl.frame = CGRectMake(15, 41, segmentWidth+20, 30);

self.testSegment = segmentedControl;