Git Product home page Git Product logo

segmentdemo's Introduction

自定义SegmentControl(CustomSegmentControl)

背景

iOS提供的控件UISegmentControl可以通过setBackgroundImage、setDividerImage等改变控件的外观。但在处理圆角和按下状态时效果不太理想,所以实现了一个自定义控件,它继承UIButton,使用方法和系统UISegmentControl类似,支持点击和拖动。

使用方法

引入工程

将文件CustomSegmentControl.h,CustomSegmentControl.m两个文件拷贝到工程中,在需要引用的地方

#import "CustomSegmentControl.h"

实例化控件对象

CustomSegmentControl* segment = [[CustomSegmentControl alloc] initWithItems:@[@"日", @"周", @"月", @"年"]];
segment.frame = CGRectMake(80, 100, 200, 40);    [segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
segment.selectedIndex = 0;

/*自定义背景图片
  segment.normalImage = [[UIImage imageNamed:@"mySegCtrl-normal-bkgd"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 20, 2, 20) resizingMode:UIImageResizingModeStretch];
  segment.selectedImage = [[UIImage imageNamed:@"mySegCtrl-selected-bkgd"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 20, 2, 20) resizingMode:UIImageResizingModeStretch];
 */
     
[self.view addSubview:segment];
    
segment = [[CustomSegmentControl alloc] initWithItems:@[@"资料库", @"更新"]];
segment.frame = CGRectMake(80, 200, 200, 40);
segment.selectedIndex = 0;
[self.view addSubview:segment];

控件效果

效果图

segmentdemo's People

Contributors

aiijim avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.