2015-09-29 89 views
1

我正在嘗試向選定的分段控件添加邊框。目前,我有代碼設置的3px的邊框,以整個分段控制的底部:將邊框添加到SELECTED分段控件的底部

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]; 

不過,我想這個邊境僅添加到選定的分段控制。我該怎麼做呢?

回答

-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]; 

    } 
} 
+0

爲什麼在所有三種情況下都做同樣的操作?爲什麼你不能在方法體中寫一次?就像我在回答中寫的那樣。這個答案似乎是前兩個答案的混合,看起來很混亂。如果您正在動態計算幀,則不需要使用if else塊 –

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操作方法,就可以改變這些變化

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圖層作爲成員變量。

+0

這工作,但我無法弄清楚如何使bottomBorder層一個成員變量來說明何時選擇一個新的選擇。你能給我舉個例子嗎?謝謝! –

+0

@ JoshO'Connor:只要這樣做:'@implementation YourClass {CALayer * bottomBorder;}' –

+0

這非常接近100%正確。我們所需要做的就是把這個連接到一個連接到分段控制的動作上。看到我的答案。 –

1

我轉換的Objective-C的解決方案集成到swift3這是爲我工作,我的段控制器寬度等於查看寬度

@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!) 
}