UISegmentedControl does not respect separator images set for UIControlStateDisabled

I am using the new API UIAppearancein iOS 5 for a style UISegmentedControlwith custom graphics. I need some segments to be disabled sometimes at runtime, but the methods UIAppearancedo not seem to allow me to set the delimiter image to state UIControlStateDisabled.

I'm calling:

[[UISegmentedControl appearance] setDividerImage:disabledSelectedImage
                                    forLeftSegmentState:UIControlStateDisabled
                                    rightSegmentState:UIControlStateSelected
                                    barMetrics:UIBarMetricsDefault];

where disabledSelectedImageis the resized image from this resource:

disabled-selected-image

However, when I set the left segment to disable ( [UISegmentedControl setEnabled:forSegmentAtIndex:]), the result will be the following:

UISegmentedControl UI glitch

You can clearly see that UISegmentedControlthe default separator image UIControlStateNormalis - UIControlStateNormal.

It seems completely happy to set the background image using UIControlStateDisabled

[[UISegmentedControl appearance] setBackgroundImage:disabledImage
                                    forState:UIControlStateDisabled
                                    barMetrics:UIBarMetricsDefault];

( , , ), . - ?

+5
6

, iOS Apple radar Apple. , , .

+3

, , .

UISegmentedControl :

@implementation MJSegmentedControl

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger cachedIndex = self.selectedSegmentIndex;
    self.selectedSegmentIndex = 0;
    self.selectedSegmentIndex = cachedIndex;
}

@end
+1

iOS 5, , , . , . , .

0

, . ( ).

XIB . XIB , -viewDidLoad.

UIView, XIB. -awakeFromNib UIView. .

0

: UISegmentedControl iOS 5

UISegmentedControl :

dispatch_async(dispatch_get_main_queue(),^{
    // disable part of the segmented control
    [self.eventScopeSegmentedControl setEnabled:NO forSegmentAtIndex:2];
});

viewDidLoad, , , . , , , , - .

, viewWillAppear ( super: viewWillAppear) ( viewWillLoad), , .

0

. , , , , , . , UIView " ".

:

  • UIView UISegmentedControl. , UIView UISegmentedControl
  • UIView, .
  • UIView UISegmentedControl
  • UIView UISegmentedControl
  • UIView, UISegmentedControl

UISegmentedControl , UIView UISegmentedControl.

UISegmentedControl , UIView UISegmentedControl.

UISegmentedControl.enabled.

, , , UISegmentedControl 1 , .

Here's what my custom segmented control looks like when applying this solution:

Enabled Segmented Management

enabled segmented control

Disabled Segmented Management

disabled segmented control

Here are some interesting snippets of code:

The UIView form corresponds to the UISegementedControl (load time configuration)

  UISegmentedControl* segmentedControl = ...
  //Segmented Control disabled visual cue view
  UIView* view = ...

  //Step #2       
  view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];

  //Step #3 and #4
  view.frame = segmentedControl.frame;

  //Step #5
  view.layer.cornerRadius = 5
  view.clipsToBounds = YES;

  //Ensure this is disabled by default
  view.userInteractionEnabled = NO;

Enable / Disable UISegementedControl (Runtime Change)

BOOL segmentedControlEnabled = ...

if(segmentedControlEnabled) {
  segmentedControl.userInteractionEnabled = YES;
  view.hidden = YES;    
} else {     
  segmentedControl.userInteractionEnabled = NO;
  view.hidden = NO;
}

What is it.

-

0
source

All Articles