内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

UISegmentedControl

2018-06-08 18:43 出处:清屏网 人气: 评论(0

一、简介

<<分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换

<<继承关系:UISegmentedControl-->UIControl-->UIView-->UIResponder-->NSObject

格式为

1-->初始化(作用)

typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {

UIDataDetectorTypePhoneNumber = 1 << 0, //检测电话

UIDataDetectorTypeLink = 1 << 1, //检测网址和邮箱

UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // 检测地址

UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // 检测日历

UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // 货物追踪号码检测

UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 5, // 班机号码检测

UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 6, //用户可能要查找的信息

UIDataDetectorTypeNone = 0, // 禁用检测

UIDataDetectorTypeAll = NSUIntegerMax // 检测所有类型链接

} __TVOS_PROHIBITED;

;(如果属性有枚举类型的话,这里会有枚举类型说明)

self.webView.dataDetectorTypes =UIDataDetectorTypeAll;(这是具体的例子)

@property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);// UITextField 设置提示文字 (这是说明)

二、UISegmentedControl的初始化方法(属性的顺序与苹果API一致)

1--> 初始化方法

NSArray *array = [NSArray arrayWithObjects:@"家具",@"灯饰",@"建材",@"装饰", nil];
UISegmentedControl *segment =[[UISegmentedControl alloc]initWithItems:array];

-(instancetype)initWithItems:(nullable NSArray *)items; // items 可以是 NSStrings or UIImages. 控件的大小会自动调整为适合的内容。

2-->设置UISegmentedControl的风格

typedef enum {

UISegmentedControlStylePlain, // large plain 有灰边的大白按钮,适合偏好设置单元

UISegmentedControlStyleBordered, // large bordered 黑边的大白按钮,适用于表格单元

UISegmentedControlStyleBar, // small button/nav bar style. tintable 小按钮,适合导航栏

UISegmentedControlStyleBezeled, // large bezeled style. tintable

} UISegmentedControlStyle;

mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//风格

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;//废弃属性

3-->设置在点击后是否恢复原样

mySegmentedControl.momentary = YES;

@property(nonatomic,getter=isMomentary) BOOL momentary; //如果设置,则在跟踪结束后,我们不会继续显示选定的状态。默认是 NO

4-->获取总选项数

[segment numberOfSegments];//得到segment的数量

@property(nonatomic,readonly) NSUInteger numberOfSegments;

5-->是否根据segment的内容改变segment的宽度

segment.apportionsSegmentWidthsByContent = YES;

@property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);

6-->在指定索引插入一个选项并设置题目

[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];

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

7-->在指定索引插入一个选项并设置图片

[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];

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

8-->移除指定索引的选项

[segmentedControl removeSegmentAtIndex:0 animated:NO];

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

9-->移出所有segment

[segment removeAllSegments];

-(void)removeAllSegments;

10-->设置指定索引的题目

[segmentedControl setTitle:@"two" forSegmentAtIndex:1];

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

11-->获取指定索引选项的标题

NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];

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

12-->设置指定索引的图片

segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];

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

13-->获取指定索引选项的图片

UIImage* myImage = [mySegmentedControl imageForSegmentAtIndex:2];

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

14-->设置Item的宽度

[segmentedControl setWidth:70.0 forSegmentAtIndex:2];

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

15-->获取指定索引选项的宽度

CGFloat float =[segmentedControl widthForSegmentAtIndex:2];

-(CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;

16-->设置内容偏移

[segment setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];

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

17-->获取内容偏移

CGSize size = [mySegmentedControl contentOffsetForSegmentAtIndex:2];

-(CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;

18-->设置segment是否可用

[segmentedControl setEnabled:NO forSegmentAtIndex:4];

-(void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; // default is YES

19-->判断指定索引选项是否可选

BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];

-(BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;

20-->设置默认选择项索引

segmentedControl.selectedSegmentIndex = 2;

@property(nonatomic) NSInteger selectedSegmentIndex;

21-->设置segments的颜色

segmentedControl.tintColor = [UIColor redColor];

@property(null_resettable,nonatomic,strong) UIColor *tintColor;

22-->设置背景图

typedef enum {

UIBarMetricsDefault, //竖屏

UIBarMetricsLandscapePhone, 横屏

} UIBarMetrics;

[segment setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

-(void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

23-->获取背景图

UIImage* myImage = [mySegmentedControl backgroundImageForState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

-(nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

24-->设置标签之间分割线的图案

typedef enum {

UIBarMetricsDefault, //竖屏

UIBarMetricsLandscapePhone, 横屏

} UIBarMetrics;

[segment setDividerImage:[UIImage imageNamed:@"234"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

-(void)setDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

25-->获取标签之间分割线的图案

UIImage* myImage = [mySegmentedControl dividerImageForLeftSegmentState: UIControlStateNormal  rightSegmentState: UIControlStateNormal  barMetrics: UIBarMetricsDefault];

-(nullable UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

26-->通过Attribute字符串属性字典设置标签标题

[segC setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

-(void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

27-->获取Attribute字符串属性字典

NSDictionary *dic=[segmentedControl titleTextAttributesForState: UIControlStateNormal];

-(nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

28-->设置标签内容的偏移量

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {

UISegmentedControlSegmentAny = 0,//所有标签都受影响

UISegmentedControlSegmentLeft = 1, //只有左边部分受到影响

UISegmentedControlSegmentCenter = 2, // 只有中间部分受到影响

UISegmentedControlSegmentRight = 3, // 只有右边部分受到影响

UISegmentedControlSegmentAlone = 4, // 在只有一个标签的时候生效

};

[segC setContentPositionAdjustment:UIOffsetMake(0, -60) forSegmentType: UISegmentedControlSegmentAlone  barMetrics: UIBarMetricsDefault];

-(void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

29-->获取自定义偏移量

UIOffset offset= [segC s contentPositionAdjustmentForSegmentType: : UISegmentedControlSegmentAlone  barMetrics: UIBarMetricsDefault];

-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

分享给小伙伴们:
本文标签: UISegmentedC

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号