iOS-UISegmentedControl-隱藏邊框


 

原理: 

1. 用tintColor屬性,把整個UISEgmentControl 設置成為透明色.

2. 設置正常狀態下的titleTextAttributes.和選中狀態下的titleTextAttributes.

#import "SecondViewController.h"
#import "Masonry.h"

@interface SecondViewController ()

@property (nonatomic, strong) UISegmentedControl * segmentedControl_one;

@end

@implementation SecondViewController

#pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self basicSetting];
    
    [self addSegmentedControl_one];
    
}

#pragma mark - 系統代理

#pragma mark - 點擊事件
- (void)segmentedControl_one:(UISegmentedControl *)sender
{
    NSLog(@"index: %ld",(long)sender.selectedSegmentIndex);
    
}

#pragma mark - 實現方法
#pragma mark 基本設置
- (void)basicSetting
{
    self.title = @"隱藏邊框";
}

- (void)addSegmentedControl_one
{
    [self.view addSubview:self.segmentedControl_one];
    [self.segmentedControl_one mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view).with.offset(10);
        make.right.mas_equalTo(self.view).with.offset(-10);
        make.top.mas_equalTo(self.view).with.offset(30);
        make.height.mas_equalTo(40);
    }];
}


#pragma mark - setter & getter
- (UISegmentedControl *)segmentedControl_one
{
    if (!_segmentedControl_one)
    {
        NSArray * array = @[@"第一段",@"第二段",@"第三段",@"第四段"];
        self.segmentedControl_one = [[UISegmentedControl alloc] initWithItems:array];
        
        // 去掉顏色,現在整個segment偶看不到,可以相應點擊事件
        self.segmentedControl_one.tintColor = [UIColor clearColor];
        
        // 正常狀態下
        NSDictionary * normalTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:16.0f],NSForegroundColorAttributeName : [UIColor grayColor]};
        [self.segmentedControl_one setTitleTextAttributes:normalTextAttributes forState:UIControlStateNormal];
        
        // 選中狀態下
        NSDictionary * selctedTextAttributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:20.0f],NSForegroundColorAttributeName : [UIColor redColor]};
        [self.segmentedControl_one setTitleTextAttributes:selctedTextAttributes forState:UIControlStateSelected];
        
        
        [self.segmentedControl_one addTarget:self action:@selector(segmentedControl_one:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl_one;
}

@end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 ios UISegmentedControl 用法 【轉】 UISegmentedControl設置樣式(圓角,邊框顏色) iOS:分段控件UISegmentedControl的詳細使用 IOS ——UI篇—— UISegmentedControl的用法總結 隱藏 input 標簽的邊框 在input中既隱藏邊框,也隱藏輪廓的設置 css隱藏input邊框陰影 WPF中的TextBox隱藏邊框
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM
標簽隱藏內邊框與外邊框 iOS開發之分段控制器(UISegmentedControl)