Как настроить UIToolbar с кнопками, содержащими цветные изображения?

У меня есть два вопроса по UIToolbar:

1: Я прочитал множество ответов Stackoverflow о том, как использовать кнопки с пользовательскими изображениями (цветными) в UIToolbar. Я попытался разместить представление (взломать) поверх UIToolbar и поместить в него кнопки с изображениями, но не удалось. Прямо сейчас я застрял. Как этого добиться?

2: Есть ли способ, чтобы несколько кнопок одновременно находились в "нажатом" состоянии? Функция, которую я хочу выполнить, - это разные кнопки с разными видами сортировки.


person Christofffer    schedule 12.01.2011    source источник


Ответы (2)


Хорошо, ответ разрешился сам собой ... вот он:

Могу ли я получить UIBarButtonItem с цветным изображением?

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated]; 
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];

    //Calculate the height of the toolbar
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view.
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver
    [toolbar setFrame:rectArea];

    //Create a button
    UIImage *image = [UIImage imageNamed:@"colorImage.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    [toolbar setItems:[NSArray arrayWithObjects:barButtonItem,nil]];

    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];
}

-(void)myAction{
    NSLog(@"jippiii");
}
person Christofffer    schedule 12.01.2011

Я знаю ответ на ваше второе требование.

в ИБ щелкните вид и в инспекторе отметьте включение нескольких касаний.

Ваше здоровье

person rdp    schedule 12.01.2011