iOS开发 UISegmentedControl的使用

2025-11-03 02:51:08

1、创建工程项目和视图控制器

      1、创建一个Sing View Application工程项目;

      2、选中Main.storyboard进行编辑;

      3、添加Navigation Controller并将箭头指向它;

      4、将Navigation Controller的rootViewController指向View Controller。

iOS开发 UISegmentedControl的使用

2、创建UISegmentedControl

      1、初始化:- (instancetype)initWithItems:(nullable NSArray *)items;

      2、设置各种属性;

      3、添加UISegmentedControl响应代理。

iOS开发 UISegmentedControl的使用

3、实现UISegmentedControl响应事件代理

     - (void)segmentedControlAction:(UISegmentedControl *)segmentedControl;

iOS开发 UISegmentedControl的使用

4、将segmentedControl添加到navigationBar上

      1、设置Frame:

      segmentedControl.frame = CGRectMake(x, y, width, height);

      2、设置navigationItem.titleView为segmentedControl:

      self.navigationItem.titleView = segmentedControl;   

iOS开发 UISegmentedControl的使用

5、运行效果图

      1、插入了一个“插入分段”文本项;

      2、插入了一个UIImage项;

      3、删除了“分段3”项;

      4、将下标为2的(“插入分段”项)的宽度改成为100.0,其他的平均;

      5、将下标为2的(“插入分段”项)偏移y+5.0。

iOS开发 UISegmentedControl的使用

6、如果您喜欢,请按投票;如果有疑问,欢迎一起探讨。

iOS开发 UISegmentedControl的使用

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢