سؤال

I have searched around an figured out how to add a UISegmentedControl item to the navigation bar button item (right bar button). It works fine in the simulator but when I try it on the device, only the first item in the segmented control shows up. It occupies the full length (i.e. there is only one segment across the whole thing).

- (void) setupSegmentedControl {
//set up the segmented control and add it to the nav bar rightBartButtonItem
UISegmentedControl * segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray         arrayWithObjects:[UIImage imageNamed:@"Settings.png"],[UIImage imageNamed:@"Map-Icon.png"],[UIImage imageNamed:@"Search.png"], nil]];
UIBarButtonItem * segmentControlButton = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
[segmentControl setBackgroundColor:[UIColor clearColor]];
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.frame = CGRectMake(0, 0, 75, 30);
[segmentControl setMomentary:YES];
[segmentControl addTarget:self
                   action:@selector(segmentedControlAction:)
         forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = segmentControlButton;
[segmentControl release];

}

Any ideas what going wrong?

هل كانت مفيدة؟

المحلول

Make sure that the image filenames match exactly with the resource names in your project including uppercase/lowercase letters.

The device is case-sensitive and if the name doesn't match, imageNamed: will return nil terminating the array.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top