UISegmentedControl图像突出显示iOS中的错误?

时间:2014-01-29 13:35:00

标签: ios iphone ipad uisegmentedcontrol

我有一个应用程序,我想使用Instagram中的按钮自定义分段控件。我这样做了

 [[UISegmentedControl appearanceWhenContainedIn:[UISearchBar class], nil] setSegmentedControlStyle:UISegmentedControlStyleBar];
 [[UISegmentedControl appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor clearColor]];

 [[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"back_bg.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
 [[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"white_btn.png"]

                   forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

 [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"segcontrol_uns-sel.png"]
                                     forLeftSegmentState:UIControlStateNormal
                                       rightSegmentState:UIControlStateSelected
                                              barMetrics:UIBarMetricsDefault];
 [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"segcontrol_sel-uns.png"]
                                     forLeftSegmentState:UIControlStateSelected
                                       rightSegmentState:UIControlStateNormal
                                              barMetrics:UIBarMetricsDefault];

但是当我选择按钮时,它除了分割图像外突出显示。所以看起来有线。有人可以帮我吗?

4 个答案:

答案 0 :(得分:1)

试试这个:

UIImage *defaultBgImage,*selectedBgImage,*segcontrol_uns,*segcontrol_sel;

if ([UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)]) 
{
    defaultBgImage = [[UIImage imageNamed:@"back_bg.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    selectedBgImage = [[UIImage imageNamed:@"white_btn.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    segcontrol_uns = [[UIImage imageNamed:@"segcontrol_uns-sel.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    segcontrol_sel = [[UIImage imageNamed:@"segcontrol_sel-uns.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
else
{
    defaultBgImage = [UIImage imageNamed:@"back_bg.png"];
    selectedBgImage = [UIImage imageNamed:@"white_btn.png"];
    segcontrol_uns = [UIImage imageNamed:@"segcontrol_uns-sel.png"];
    segcontrol_sel = [UIImage imageNamed:@"segcontrol_sel-uns.png"];
}

[[UISegmentedControl appearance] setBackgroundImage:defaultBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setBackgroundImage:selectedBgImage
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

 [[UISegmentedControl appearance] setDividerImage:segcontrol_uns forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected
  barMetrics:UIBarMetricsDefault];

 [[UISegmentedControl appearance] setDividerImage:segcontrol_sel forLeftSegmentState: UIControlStateSelected rightSegmentState:UIControlStateNormal
  barMetrics:UIBarMetricsDefault];

答案 1 :(得分:1)

你需要做" setBackgroundImage "四个不同的州。

  1. UIControlStateNormal
  2. UIControlStateSelected
  3. UIControlStateHighlighted
  4. UIControlStateSelected | UIControlStateHighlighted(按下已选择的段时)
  5. 并且" setDividerImage "七个不同的州。

    1. forLeftSegmentState:的 UIControlStateNormal rightSegmentState:的 UIControlStateSelected
    2. forLeftSegmentState: UIControlStateSelected rightSegmentState: UIControlStateNormal
    3. forLeftSegmentState: UIControlStateHighlighted rightSegmentState: UIControlStateSelected
    4. forLeftSegmentState: UIControlStateSelected rightSegmentState: UIControlStateHighlighted
    5. forLeftSegmentState: UIControlStateSelected | UIControlStateHighlighted rightSegmentState: UIControlStateNormal
    6. forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateSelected | UIControlStateHighlighted
    7. forLeftSegmentState: UIControlStateNormal rightSegmentState: UIControlStateNormal

答案 2 :(得分:0)

三段需要以下图像 1.段bg被选中 2.段bg未选中 3.中段,左选,右选未选 4.中段,左未选,右选 分段中间,左侧和左侧正确选择 分段中间,左侧和左侧未经选择的

我已经为3段控件编写了这段代码。但是,您可以根据自己的需要进行调整。

/* Unselected background */
UIImage *unselectedBackgroundImage = [[UIImage imageNamed:@"segment_background_unselected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[[UISegmentedControl appearance] setBackgroundImage:unselectedBackgroundImage
                                       forState:UIControlStateNormal
                                     barMetrics:UIBarMetricsDefault];

/* Selected background */
UIImage *selectedBackgroundImage = [[UIImage imageNamed:@"segment_background_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[[UISegmentedControl appearance] setBackgroundImage:selectedBackgroundImage
                                       forState:UIControlStateSelected
                                     barMetrics:UIBarMetricsDefault];

/* Image between two unselected segments */
UIImage *bothUnselectedImage = [[UIImage imageNamed:@"segment_middle_unselected"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
[[UISegmentedControl appearance] setDividerImage:bothUnselectedImage
                         forLeftSegmentState:UIControlStateNormal
                           rightSegmentState:UIControlStateNormal
                                  barMetrics:UIBarMetricsDefault];

/* Image between segment selected on the left and unselected on the right */
UIImage *leftSelectedImage = [[UIImage imageNamed:@"segment_middle_left_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
[[UISegmentedControl appearance] setDividerImage:leftSelectedImage
                         forLeftSegmentState:UIControlStateSelected
                           rightSegmentState:UIControlStateNormal
                                  barMetrics:UIBarMetricsDefault];

/* Image between segment selected on the right and unselected on the left */
UIImage *rightSelectedImage = [[UIImage imageNamed:@"segment_middle_right_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
[[UISegmentedControl appearance] setDividerImage:rightSelectedImage
                         forLeftSegmentState:UIControlStateNormal
                           rightSegmentState:UIControlStateSelected
                                  barMetrics:UIBarMetricsDefault];

答案 3 :(得分:0)

以下代码为我修好了:

[[UISegmentedControl appearanceWhenContainedIn:[InboxViewController class], nil]  setBackgroundImage:[UIImage imageNamed:@"btn-gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearanceWhenContainedIn:[InboxViewController class], nil]  setBackgroundImage:[UIImage imageNamed:@"btn-gray"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearanceWhenContainedIn:[InboxViewController class], nil]  setBackgroundImage:[UIImage imageNamed:@"btn-gray"] forState:UIControlStateSelected | UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearanceWhenContainedIn:[InboxViewController class], nil]  setBackgroundImage:[UIImage imageNamed:@"btn-gray"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];