【问题标题】:UISegmentControl bottom line on selected index所选索引上的 UISegmentedControl 底线
【发布时间】:2016-06-17 16:04:42
【问题描述】:

我的UISegmentControl 没有边框和段分隔符。我想在所选段下方放置一条线。我试过这样做:

CALayer *bottomLayer = [CALayer layer];
bottomLayer.borderColor = [UIColor blackColor].CGColor;
bottomLayer.borderWidth = 3;

// Calculating frame
CGFloat width            = segment.frame.size.width/segment.numberOfSegments;
CGFloat x                = 0;
CGFloat y                = segment.frame.size.height - 5;
bottomLayer.frame       = CGRectMake(x, y,width, 2);

[bottomLayer setName:@"bottom"];
// Adding selection to segment
[segment.layer addSublayer:bottomLayer];  

当我点击该段时,这会放置一条线。但是当我松开水龙头时,再也看不到线条了。我使用 UIControlEventValueChanged 作为 controlEvent。
我如何在不使用第三方的情况下实现这一目标。

【问题讨论】:

  • 没有第三方就无法实现,但我建议您使用按钮和图像视图进行线路。使用 uiview 动画为按钮下的 imageview 设置动画。这是最好和最简单的方法。

标签: ios objective-c iphone uisegmentedcontrol


【解决方案1】:

UISegmentedControl 是一个继承自 UIView 的类,因此您可以向其中添加/删除子视图。因此,我建议不要使用 CALayer,而是使用 UIView 或 UIImageView 将线放在下面。

UIView *bottomView = [[UIView alloc] init];
bottomView.backgroundColor = [UIColor blackColor];

// Calculating frame
CGFloat width            = segment.frame.size.width/segment.numberOfSegments;
CGFloat x                = 0;
CGFloat y                = segment.frame.size.height - 5;
bottomView.frame         = CGRectMake(x, y,width, 2);

// Adding selection to segment
[segment addSubview:bottomView];  

全局实例化您的子视图,以便您可以在UIControlEventValueChanged 删除它们或更改其不透明度(即bottomView.alpha = 0),

【讨论】:

    【解决方案2】:

    您可以通过选择subviewsegment control 来实现此目的。这是示例代码,将其放入 viewDidLoad 以用于默认选定段并放入 UIControlEventValueChanged-

    CALayer *bootomBorder = [CALayer layer];
    bootomBorder.backgroundColor = [UIColor greenColor].CGColor;
    bootomBorder.frame = CGRectMake(0, userProfileSagmentOutlet.subviews[userProfileSagmentOutlet.selectedSegmentIndex].frame.size.height-1, userProfileSagmentOutlet.frame.size.width/userProfileSagmentOutlet.numberOfSegments-2, 1.0f);
    [userProfileSagmentOutlet.subviews[userProfileSagmentOutlet.selectedSegmentIndex].layer addSublayer:bootomBorder];
    

    您也可以参考此链接,这可能会对您有所帮助-https://stackoverflow.com/a/37705692/5097148

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多