Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show the right way of adding a badge to a UISegmentedControl #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sample App/iPhoneMK Sample App/NumberBadgeViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@property (retain, nonatomic) IBOutlet UIToolbar* myToolbar;
@property (retain, nonatomic) MKNumberBadgeView* badgeSeven;
@property (retain, nonatomic) MKNumberBadgeView* badgeEight;
@property (retain, nonatomic) MKNumberBadgeView* badgeNine;

-(IBAction)slideValueChanged:(id)sender;

Expand Down
43 changes: 34 additions & 9 deletions Sample App/iPhoneMK Sample App/NumberBadgeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,57 @@ - (void)viewDidLoad

// Badge Seven
UIButton* buttonSeven = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
buttonSeven.titleLabel.text = @"seven";
[buttonSeven setTitle:@"seven" forState:UIControlStateNormal];
[buttonSeven setBackgroundColor:[UIColor blueColor]];
self.badgeSeven = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(buttonSeven.frame.size.width - 37,
-15,
74,
30)];
self.badgeSeven.font = [UIFont boldSystemFontOfSize:11];
[buttonSeven addSubview:self.badgeSeven];

// Badge Eight
// Badge Eight - Wrong way
self.badgeEight = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(-22,
-15,
44,
30)];
UISegmentedControl* segmentedControl =
[[UISegmentedControl alloc] initWithItems:@[@"9a",@"9b", @"9c"]];
[segmentedControl addSubview:self.badgeEight];

// Add buttons seven and eight to the toolbar
UIBarButtonItem* buttonEight = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.badgeEight.font = [UIFont boldSystemFontOfSize:11];
UISegmentedControl* segmentedControlEight =
[[UISegmentedControl alloc] initWithItems:@[@"9a",@"9b",@"9c"]];
segmentedControlEight.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControlEight.momentary = YES;
[segmentedControlEight addSubview:self.badgeEight];
UIBarButtonItem* buttonEight = [[UIBarButtonItem alloc] initWithCustomView:segmentedControlEight];

// Badge Nine - Right Way
self.badgeNine = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(-22,
-15,
44,
30)];
self.badgeNine.font = [UIFont boldSystemFontOfSize:11];
UISegmentedControl* segmentedControlNine =
[[UISegmentedControl alloc] initWithItems:@[@"9a",@"9b",@"9c"]];
segmentedControlNine.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControlNine.momentary = YES;
//segmentedControlNine.tintColor = [UIColor blackColor]; // may need to be set explicitly because of container indirection
UIView* containerViewNine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
[containerViewNine addSubview:segmentedControlNine];
[containerViewNine addSubview:self.badgeNine];
UIBarButtonItem* buttonNine = [[UIBarButtonItem alloc] initWithCustomView:containerViewNine];

// Add seven, eight and nine to the toolbar

NSArray* toobarItems =
@[
[[UIBarButtonItem alloc] initWithCustomView:buttonSeven],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil],
buttonEight
buttonEight,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil],
buttonNine
];
[self.myToolbar setItems:toobarItems];

Expand Down Expand Up @@ -152,6 +176,7 @@ -(IBAction)slideValueChanged:(id)sender {
self.badgeSix.value = sliderValue * 1000;
self.badgeSeven.value = sliderValue * 1000;
self.badgeEight.value = sliderValue;
self.badgeNine.value = sliderValue;
}

@end