UIKit 框架之UISegmentedControl

//
//  ViewController.m
//  UISegmentedControl
//
//  Created by City--Online on 15/5/20.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UISegmentedControl *segmentedControl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _segmentedControl =[[UISegmentedControl alloc]initWithFrame:CGRectMake(10, 100, 200, 50)];
    //属性无效
//    _segmentedControl.segmentedControlStyle=UISegmentedControlStyleBar;

    //点击后是否恢复原样
    _segmentedControl.momentary=NO;
    //是否根据segment的内容改变segment的宽度
    _segmentedControl.apportionsSegmentWidthsByContent=YES;

    //插入
    [_segmentedControl insertSegmentWithTitle:@"0" atIndex:0 animated:YES];
    [_segmentedControl insertSegmentWithTitle:@"1" atIndex:1 animated:YES];
    [_segmentedControl insertSegmentWithTitle:@"2" atIndex:2 animated:YES];
    //添加时若插入索引已经存在,则已最新的索引为准重新排序 例如下面的
    [_segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"1.jpg"] atIndex:0 animated:YES];
    [_segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"2.jpg"] atIndex:1 animated:YES];
    //删除时也是和插入一样 删除一个之后重新排序
    [_segmentedControl removeSegmentAtIndex:0 animated:YES];
    [_segmentedControl removeSegmentAtIndex:0 animated:YES];
    //删除所有的块
//    [_segmentedControl removeAllSegments];

    //标题
    [_segmentedControl setTitle:@"001" forSegmentAtIndex:0];
    NSString *titleIndex2=[_segmentedControl titleForSegmentAtIndex:2];
    NSLog(@"title=%@",titleIndex2);

    //图片
    [_segmentedControl setImage:[UIImage imageNamed:@"1.jpg"] forSegmentAtIndex:1];
    UIImage *imgIndex1=[_segmentedControl imageForSegmentAtIndex:1];
    NSLog(@"UIImage=%@",imgIndex1);

    //宽度
    [_segmentedControl setWidth:30 forSegmentAtIndex:2];
    //如果未设置则获取的为0
    float widthIndex1=[_segmentedControl widthForSegmentAtIndex:2];
    NSLog(@"width=%lf",widthIndex1);

    //内容偏移量
    [_segmentedControl setContentOffset:CGSizeMake(5, 5) forSegmentAtIndex:0];
    CGSize size=[_segmentedControl contentOffsetForSegmentAtIndex:2];
    NSLog(@"contentOffset=%@",NSStringFromCGSize(size));

    //可用性
    [_segmentedControl setEnabled:NO forSegmentAtIndex:0];
    BOOL enable=[_segmentedControl isEnabledForSegmentAtIndex:0];
    NSLog(@"%d",enable);

    //选中的索引值
    _segmentedControl.selectedSegmentIndex=2;

    //设置segmented的颜色
    _segmentedControl.tintColor=[UIColor redColor];

    //设置文本样式
    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSStrikethroughStyleAttributeName:@1};
    [_segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];

    //获取分段控件的个数
   NSInteger count= [_segmentedControl numberOfSegments];
    NSLog(@"numberOfSegments=%ld",count);

    [_segmentedControl addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_segmentedControl];

}

-(void)valueChanged
{
    NSLog(@"selectedIndex=%ld",_segmentedControl.selectedSegmentIndex);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

时间: 2024-07-29 06:01:36

UIKit 框架之UISegmentedControl的相关文章

iOS UIKit 框架 346 篇文档分类整理 - 预告

iOS UIKit 框架 346 篇文档分类整理 - 预告 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分

UIKit 框架之UIView二

下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {

UIKit 框架之Bar、Controller

UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarItems,再加上UIViewController.UINavigationController.UITabBarController很容易搞糊涂.我看了好久,没看明白.动手敲了下才有一点感觉. 一.联系 一个UINavigationController对应着一个UINavigationBar.UITo

UIKit框架类层次图

学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

UIKit 框架之UICollectionViewController

1.自定义单元格 #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end #import "myCollectionViewCell.h" @implementa

UIKit 框架之UIView一

- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View's Visual Appearance //配置视觉展示 @property(nonatomic, copy) UIColor *backgroundColor //设置背景色 @property(nonatomic, getter=isHidden) BOOL hidden //隐藏view,默认为NO @property(nonatomic) CGFloat

UIKit 框架之UICollectionView

1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionVi

UIKit框架之NSObject

首先学习NSObject  // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (voi

UIKit 框架之UIScrollView

理解UIScrollView时可以假设这样的一个场景:在一个漆黑的屋子里的墙上有一副大的画,墙是可以上下左右滚动,一个矩形的手电筒固定着,开始时画的左上角坐标与矩形光的左上角坐标对应.人可以用手电筒的光来看这幅画. // // ViewController.m // UIScrollView // // Created by City--Online on 15/5/20. // Copyright (c) 2015年 XQB. All rights reserved. // #import "