UISegmentedControl設置樣式(圓角,邊框顏色)


UISegmentedControl *segmentedControl = [[UISegmentedControl allocinitWithItems:@[NSLocalizedString(@"gtasks"nil),NSLocalizedString(@"question_hotel"nil)]];

        segmentedControl.layer.cornerRadius = 20;

    segmentedControl.clipsToBounds = YES;

    segmentedControl.tintColor = IWColor(85155219);

    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],

                                             NSForegroundColorAttributeName: [UIColor whiteColor]};

    [segmentedControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//設置文字屬性

    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],

                                               NSForegroundColorAttributeName: [UIColor whiteColor]};

    [segmentedControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

    [segmentedControl setBackgroundImage:[UIImage createImageWithColor:IWColor(5991154)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [segmentedControl setBackgroundImage:[UIImage createImageWithColor:IWColor(85155219)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    segmentedControl.apportionsSegmentWidthsByContent = NO;

    self.navigationItem.titleView = segmentedControl;

 

 

UISegmentedControl用法詳解

2016.04.06 11:06* 字數 423 閱讀 13726評論 2喜歡 7

SegmentedControl又被稱作分段控制器

初始化方法:傳入的數組可以是字符串也可以是UIImage對象的圖片數組

// items can be NSStrings or UIImages. control is automatically sized to fit content

- (instancetype)initWithItems:(NSArray *)items;

設置控件風格:

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

注意:這個屬性已經廢棄,它的枚舉如下:

//"The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;

typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {

    UISegmentedControlStylePlain,     // large plain

    UISegmentedControlStyleBordered,  // large bordered

    UISegmentedControlStyleBar,       // small button/nav bar style. tintable

    UISegmentedControlStyleBezeled,   // DEPRECATED. Do not use this style.

} NS_DEPRECATED_IOS(2_0, 7_0, 

設置是否保持選中狀態:默認是NO,如果設置為YES,點擊結束后將不保持選中狀態.

@property(nonatomic,getter=isMomentary) BOOL momentary; 

獲取標簽個數:(只讀)

@property(nonatomic,readonly) NSUInteger numberOfSegments;

設置標簽寬度是否隨內容自適應:如果設置為NO,則所有標簽寬度一致,為最大寬度

@property(nonatomic) BOOL apportionsSegmentWidthsByContent;

插入文字標簽在index位置:

- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated

插入圖片標簽在index位置

- (void)insertSegmentWithImage:(UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated

根據索引刪除標簽

- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;

刪除所有標簽

- (void)removeAllSegments;

重設標簽標題

- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment; 

獲取標簽標題

- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;

設置標簽圖片

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment; 

獲取標簽圖片:標題的圖片只能設置一個

- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;

根據索引設置相應標簽寬度:如果設置為0.0,則為自適應,默認為此設置.

- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; 

根據索引獲取標簽寬度

- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;

設置標簽內容的偏移量:這個偏移量指的是 標簽的文字或者圖片

- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;

根據索引獲取變標簽內容的偏移量

- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;

根據所以設置標簽是否有效(默認有效)

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; 

根據索引獲取當前標簽是否有效

- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;

設置和獲取當前選中的標簽索引

@property(nonatomic) NSInteger selectedSegmentIndex;

設置標簽風格顏色:這個風格顏色會影響標簽的文字和圖片,如果設置為clearColor,那么整個segmentedControl將不會有背景和邊框

@property(nonatomic,retain) UIColor *tintColor;

設置特定狀態下segment的背景圖案 UIBarMetrics是一個枚舉,如下:(defaulf風格會充滿背景)

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

```

 

```

typedef NS_ENUM(NSInteger, UIBarMetrics) {

    UIBarMetricsDefault,

    UIBarMetricsCompact,

    UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar

    UIBarMetricsCompactPrompt,

    UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,

    UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,

};

```

獲取背景圖案

```

- (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

```

設置標簽之間分割線的圖案

```

- (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

```

獲取標簽之間分割線的圖案

```

- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

```

通過Attribute字符串屬性字典設置標簽標題

```

- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state

```

獲取Attribute字符串屬性字典

```

- (NSDictionary *)titleTextAttributesForState:(UIControlState)state

```

自行設置標簽內容的偏移量

```

- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics 

```

注意:UIOffset為偏移量,這個結構體中有兩個浮點數,分別表示水平量和豎直量;UISegmentedControlSegment類型參數是一個枚舉,如下:

```objc

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {

    UISegmentedControlSegmentAny = 0,//所有標簽都受影響

    UISegmentedControlSegmentLeft = 1,   //只有左邊部分受到影響  The capped, leftmost segment. Only applies when numSegments > 1.

    UISegmentedControlSegmentCenter = 2, // 只有中間部分受到影響Any segment between the left and rightmost segments. Only applies when numSegments > 2.

    UISegmentedControlSegmentRight = 3,  //只有右邊部分受到影響 The capped,rightmost segment. Only applies when numSegments > 1.

    UISegmentedControlSegmentAlone = 4,  //在只有一個標簽的時候生效 The standalone segment, capped on both ends. Only applies when numSegments = 1.

};

```

添加點擊事件:這里的時間是valueChanged

```

[segmentedControl addTarget:self action:@selector(sementedControlClick:) forControlEvents:UIControlEventValueChanged];

```

用segmentedControl和一個UIView簡單封裝一個選擇控制器,作為導航控制器的titleView

 

![有背景.gif](//upload-images.jianshu.io/upload_images/1295463-7c2eee327aadd8e6.gif?imageMogr2/auto-orient/strip)

去掉背景之后

![沒有背景.gif](//upload-images.jianshu.io/upload_images/1295463-2b8595bb5a6ac19a.gif?imageMogr2/auto-orient/strip)

 

![xib加載設置好約束](//upload-images.jianshu.io/upload_images/1295463-15b73d216cf12a22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 

![修改tintColor為clearColor](//upload-images.jianshu.io/upload_images/1295463-fe0f860df1ac5fbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 

```

#import "GZDFriendTrendTitleView.h"

@interface  GZDFriendTrendTitleView()

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

//底部線

@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

 

@implementation GZDFriendTrendTitleView

//初始化方法

+ (instancetype)friendTrendTitleView {

 

    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

}

//一次性設置

- (void)awakeFromNib {

   

    [self.segmentedControl setTitle:@"關注" forSegmentAtIndex:0];

    [self.segmentedControl setTitle:@"訂閱" forSegmentAtIndex:1];

//設置普通狀態下(未選中)狀態下的文字顏色和字體

    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];

    //設置選中狀態下的文字顏色和字體

    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];

//添加監聽

    [self.segmentedControl addTarget:self action:@selector(sementedControlClick) forControlEvents:UIControlEventValueChanged];

}

//監聽方法

- (void)sementedControlClick{

    //修改底部線的frame值產生動畫

    [UIView animateWithDuration:0.25 animations:^{

        self.bottomView.centerX = (self.segmentedControl.selectedSegmentIndex == 0)? self.segmentedControl.centerX * 0.5 : self.segmentedControl.centerX * 1.5;

    }];

    

}

 

```

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

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



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM