我正在尝试为选定的分段控件添加边框。目前,我有代码将3px的边界设置为整个分段控件的底部:
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
bottomBorder.frame = CGRectMake(0, self.segmentedControl.frame.size.height - bottomBorder.borderWidth, self.segmentedControl.frame.size.width, bottomBorder.borderWidth);
[self.segmentedControl.layer addSublayer:bottomBorder];
但是,我想将此边框仅添加到选定的分段控件。我该怎么做?
答案 0 :(得分:2)
您可以使用以下代码动态添加选择:
// Removing previous selection
[bottomBorder removeFromSuperlayer];
// Creating new layer for selection
bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.selectedSegmentIndex * width;
CGFloat y = self.segmentedControl.frame.size.height - bottomBorder.borderWidth;
bottomBorder.frame = CGRectMake(x, y,width, bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:bottomBorder];
选择新选项时,您需要删除先前的选择,为此,将bottomBorder
图层作为成员变量。
答案 1 :(得分:1)
我将Objective-C解决方案转换为swift3它对我有用,我的Segment控制器宽度等于View width
@IBAction func SegmentAction(_ sender: UISegmentedControl) {
self.segmentBottomBorder?.removeFromSuperlayer()
self.segmentBottomBorder = CALayer()
self.segmentBottomBorder?.borderColor = UIColor.red.cgColor
self.segmentBottomBorder?.borderWidth = 3
let x = CGFloat(sender.selectedSegmentIndex) * width
let y = sender.frame.size.height - (self.segmentBottomBorder?.borderWidth)!
let width: CGFloat = sender.frame.size.width/3
self.segmentBottomBorder?.frame = CGRect(x: x, y: y, width: width, height: (self.segmentBottomBorder?.borderWidth)!)
sender.layer.addSublayer(self.segmentBottomBorder!)
}
答案 2 :(得分:0)
这可能不是正确的方法,但你可以试一试。
在加载段控件时,请在viewdidload中尝试此操作,以便根据selectedsegment按钮添加图层
if (self.segmentedControl.selectedSegmentIndex==0) {
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor redColor].CGColor;
bottomBorder.borderWidth = 3;
bottomBorder.frame = CGRectMake(0, self.segmentedControl.frame.size.height - bottomBorder.borderWidth, self.segmentedControl.frame.size.width/2, bottomBorder.borderWidth);
[self.segmentedControl.layer addSublayer:bottomBorder];
}else if (self.segmentedControl.selectedSegmentIndex==1){
//change the border layer frame
} else if (self.segmentedControl.selectedSegmentIndex==2){
//change the border layer frame
}
再次在segmentcontrol操作方法中,您可以更改这些更改
答案 3 :(得分:-1)
//Connect action to segmented control.
- (IBAction)segmentedControlAction:(id)sender {
if(segmentedControl.selectedSegmentIndex == 0)
{
NSLog(@"First Segment (Feed) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.selectedSegmentIndex * width;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
else if(segmentedControl.selectedSegmentIndex == 1)
{
NSLog(@"Second Segment (Stations) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.segmentedControl.frame.size.width/3;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
else if(segmentedControl.selectedSegmentIndex == 2)
{
NSLog(@"Third Segment (Events) Selected");
//Remove SuperLayer when segment is selected
[_bottomBorder removeFromSuperlayer];
// Creating new layer for selection
_bottomBorder = [CALayer layer];
_bottomBorder.borderColor = [UIColor redColor].CGColor;
_bottomBorder.borderWidth = 3;
// Calculating frame
CGFloat width = self.segmentedControl.frame.size.width/3;
CGFloat x = self.view.bounds.size.width - self.segmentedControl.frame.size.width/3;
CGFloat y = self.segmentedControl.frame.size.height - _bottomBorder.borderWidth;
_bottomBorder.frame = CGRectMake(x, y,width, _bottomBorder.borderWidth);
// Adding selection to segment
[self.segmentedControl.layer addSublayer:_bottomBorder];
}
}