UIToolbar - добавить кнопки равной ширины без пробелов - PullRequest
0 голосов
/ 26 апреля 2018

У меня есть UIToolbar, и я хочу добавить к нему кнопки UIB с одинаковым размером. В настоящее время у меня есть:

CGFloat width = self.toolbarMapMenu.frame.size.width / 4.0;
CGFloat height = self.toolbarMapMenu.frame.size.height;

btn0 = [self createMenuButton:@"a" width:width height:height];
btn1 = [self createMenuButton:@"b" width:width height:height];
btn2 = [self createMenuButton:@"c" width:width height:height];
btn3 = [self createMenuButton:@"d" width:width height:height];


UIBarButtonItem *it0 = [[UIBarButtonItem alloc] initWithCustomView:btn0];
UIBarButtonItem *it1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
UIBarButtonItem *it2 = [[UIBarButtonItem alloc] initWithCustomView:btn2];
UIBarButtonItem *it3 = [[UIBarButtonItem alloc] initWithCustomView:btn3];

[self.toolbarMapMenu setItems:[NSArray arrayWithObjects:
                               it0,  it1,  it2,  it3,
                               nil]
                            ];

И где-то еще

-(UIButton *)createMenuButton: (NSString *)name width:(CGFloat) w height:(CGFloat) h
{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tintColor = [UIColor whiteColor];

    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];

    [[btn.widthAnchor constraintEqualToConstant: w] setActive:true];
    [[btn.heightAnchor constraintEqualToConstant: h] setActive:true];
    btn.translatesAutoresizingMaskIntoConstraints = false;

    return btn;
}

Однако, когда я добавляю эти кнопки, они имеют одинаковый размер, но между первой и последней кнопками есть пробел:

Примерно так (UIToolbar):

(<gap> btn0 btn1 btn2 btn3 <gap>)

Как я могу удалить этот пробел и иметь кнопку в начале панели инструментовMapMenu?

1 Ответ

0 голосов
/ 26 апреля 2018

Попробуйте добавить гибкое пространство между каждым элементом, это может решить вашу проблему и дать единообразное представление.

// Flexible Space
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[self.toolbarMapMenu setItems:[NSArray arrayWithObjects:
                               it0, flexibleItem, 
                               it1, flexibleItem, 
                               it2, flexibleItem, 
                               it3,
                               nil]];
...