IOS UISegmentedControl简介

文章目录

  • 常用属性和方法
  • 使用,页面的切换
    • AppDelegate.m
  • SubViewController.m颜色随机
  • MainViewController.m

在许多的应用程序中,开发者会加入一些主题选项以便对显示的信息做进一步分类。例如,App Store应用程序的排行榜导航栏上就显示着”付费”、”免费”、”畅销排行”选项,而这种展示方式就是通过UISegmentedControl来实现的。UISegmentedControl的使用方法类似于UIButton, 它不仅可以提供多个选择操作, 并且也可以响应交互事件。UISegmentedControl就是继承自UIButton的。
IOS UISegmentedControl简介_第1张图片

常用属性和方法

在UISegmentedControl类中提供了用于初始化UISegmentedControl对象以及设置其外观的相关属性,下面的一些是在实际开发中比较常用的。

实例化方法。该方法中需要传递一个数组类型的items参数,在该参数中填写需要展示的标题(NSString类)或者图片(UIImage类)
-(instancetype)initWithItems:(nullable NSArray *)items;
当前选中的选项索引
@property(nonatomic) NSInteger selectedSegmentIndex;
UISegmentedControl渲染的颜色
@property(null_resettable,nonatomic,strong) UIColor *tintColor;
设置指定索引的标题
-(void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment;
设置指定索引的图片
-(void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
设置指定索引选项的宽度
-(void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;

使用,页面的切换

需要一些子控制器,把子控制器的view放在UIScrollView里,通过点击实现滑动效果。
scrollView的contentOffSet属性和segmentedControl的selectedSegmentIndex交互。
应用打开首先是个导航控制器。

AppDelegate.m

//
//  AppDelegate.m
//  UISegmentedControl
//
//  Created by 谢鑫 on 2019/7/17.
//  Copyright © 2019 Shae. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    MainViewController *mvc=[[MainViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:mvc];
    nav.navigationBar.translucent=NO;
    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController=nav;
    [self.window makeKeyAndVisible];
    return YES;
}

SubViewController.m颜色随机

//
//  SubViewController.m
//  UISegmentedControl
//
//  Created by 谢鑫 on 2019/7/17.
//  Copyright © 2019 Shae. All rights reserved.
//

#import "SubViewController.h"

@interface SubViewController ()

@end

@implementation SubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:(arc4random()%255)/255.0 green:(arc4random()%255)/255.0 blue:(arc4random()%255)/255.0 alpha:1.0];
}


@end

MainViewController.m

//
//  MainViewController.m
//  UISegmentedControl
//
//  Created by 谢鑫 on 2019/7/17.
//  Copyright © 2019 Shae. All rights reserved.
//


#import "MainViewController.h"
#import "SubViewController.h"
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kNumber (self.segmentedControl.numberOfSegments)
@interface MainViewController ()
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UISegmentedControl *segmentedControl;
@end

@implementation MainViewController
- (UIScrollView *)scrollView{
    if (_scrollView==nil) {
        _scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollView.contentSize=CGSizeMake(kScreenWidth*kNumber, 0);
        _scrollView.backgroundColor=[UIColor yellowColor];
        _scrollView.pagingEnabled=YES;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate=self;
    }
    return _scrollView;
}
- (UISegmentedControl *)segmentedControl{
    if (_segmentedControl==nil) {
        _segmentedControl=[[UISegmentedControl alloc]initWithItems:@[@"最新",@"最热",@"经典"]];
        _segmentedControl.tintColor=[UIColor redColor];
        _segmentedControl.selectedSegmentIndex=0;
        [_segmentedControl addTarget:self action:@selector(changeIndex) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX=self.scrollView.contentOffset.x;
    CGFloat x=offsetX/kScreenWidth;
    int index=(int)round(x);
    self.segmentedControl.selectedSegmentIndex=index;
}
-(void)changeIndex{
    NSInteger index=self.segmentedControl.selectedSegmentIndex;
    self.scrollView.contentOffset=CGPointMake(index*kScreenWidth, 0);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView=self.segmentedControl;
    [self.view addSubview:[self scrollView]];
    for (int i=0; i

IOS UISegmentedControl简介_第2张图片
代码:https://github.com/ShaeZhuJiu/UISegmentedControl_base.git

你可能感兴趣的:(IOS)