【问题标题】:how to set color for segments in segemented control for selected state in ios如何为ios中选定状态的分段控件中的段设置颜色
【发布时间】:2016-09-21 12:55:38
【问题描述】:

我使用分段控件而不是按钮。如何设置选定段的背景颜色。我正在尝试更改背景颜色,但它的颜色出现在所有片段中。

【问题讨论】:

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

如果您设置选择的第一个索引然后编写此代码。

[segmentControl setSelectedSegmentIndex:0];

如果你设置了背景颜色然后写这段代码

UIColor *selectedColor = [UIColor whiteColor];
   for (UIControl *subview in [segmentControl subviews]) {

        [subview setTintColor:selectedColor];

    }

如果你改变了色调颜色和字体大小,那么就写下这段代码。

NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f]};
[segmentControl setTitleTextAttributes:attributes
                              forState:UIControlStateNormal];

[segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                         NSForegroundColorAttributeName:[UIColor whiteColor]}
                              forState:UIControlStateNormal];
[segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                         NSForegroundColorAttributeName:[UIColor redColor]}
                              forState:UIControlStateSelected];

【讨论】:

    【解决方案2】:

    我为您的问题尝试了示例一。我在 XIB 中设置了段控件。我还设置了三个标题。我用属性和操作将段连接到 ViewController.h。

    ViewController.h

    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController
    {
    }
    @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentBgColorChange;
    - (IBAction)actionChangeBGColor:(id)sender;
    @end
    

    ViewController.m

    #import "ViewController.h"
    @interface ViewController ()
    {
    }
    @end
    @implementation ViewController
    @synthesize segmentBgColorChange;
    
    // Then in action methods
    
    - (IBAction)actionChangeBGColor:(id)sender
    {
      UISegmentedControl *seg = sender;
      for (int i=0; i<[seg.subviews count]; i++) {
        if ([[seg.subviews objectAtIndex:i]isSelected]) {
            UIColor *bgColor = [UIColor redColor];
            [[seg.subviews objectAtIndex:i] setTintColor:bgColor];
        } else {
            [[seg.subviews objectAtIndex:i] setTintColor:nil];
        }
       }
     }
    

    【讨论】:

      【解决方案3】:

      首先,创建您选择和取消选择的颜色;

      UIColor *selectedColor = [UIColor blackColor];
      UIColor *deselectedColor = [UIColor whiteColor];
      

      然后,找到选定的段索引并给它颜色;

      for (UIControl *subview in [YourSegmentedControl subviews]) {
          if ([subview isSelected]) 
             [subview setTintColor:selectedColor]; 
          else
             [subview setTintColor:deselectedColor]; 
      }
      

      【讨论】:

      • 您是否从此处复制了解决方案? stackoverflow.com/a/12104313/661022
      • 不,从我的一个项目中复制过来的。
      • 好吧,无论如何,我投票赞成将其标记为重复,这是一个低质量问题
      猜你喜欢
      • 2012-10-07
      • 2013-08-25
      • 2015-10-06
      • 2016-04-08
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多