объединение UISegmentedControl и UIBarButtonItem в навигационном контроллере с настраиваемым фоновым изображением barbuttonitem - PullRequest
0 голосов
/ 29 ноября 2010

У меня есть barbuttonitem, который сделан программно над контроллером навигации.Я хочу отобразить его заголовок и фон, а также выделенный эффект, когда я нажимаю кнопку панели.Вот код, который я использую:

NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:@"bunga.jpg"];

_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0;
_docSegmentedControl.autoresizingMask =  UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:@selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];

UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"button.png"];
UIImage *buttonPressedImage = [UIImage imageNamed:@"buttonPressed.png"];
[[barButton titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[barButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[barButton setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[barButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[barButton titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

 CGRect buttonFrame = CGRectMake(0, 0, 110.0, 40);
 //buttonFrame.size.width = 110.0;
 //buttonFrame.size.height = buttonFrame.size.height;
[barButton setFrame:buttonFrame];
[barButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[barButton setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[barButton setTitle:docSegmentFileName forState:UIControlStateNormal];
[barButton addTarget:self action:@selector(docSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
[barBackground addSubview:barButton];

UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = @"";
[self.navItem.leftBarButtonItem setCustomView:barBackground];

К сожалению, это не работает.Вместо отображения UIBarButtonItem он просто исчезает (становится на 100% прозрачным).Когда я опускаю метод setCustomView, появляется UIBarButtonItem, но не настраивается.Как я могу решить эту проблему?

thank's ...

1 Ответ

0 голосов
/ 10 апреля 2011

Вы удаляете UISegmentedControl из UIBarButtonItem при вызове [self.navItem.leftBarButtonItem setCustomView:barBackground];.Вам нужно изменить UISegmentedControl, а не UIBarButtonItem.

...