gpt4 book ai didi

ios - UIToolbar - 添加无间隙的等宽按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:13 24 4
gpt4 key购买 nike

我有 UIToolbar,我想给它添加相同大小的 UIButtons。目前,我有:

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>)

我怎样才能消除这个差距并从 toolbarMapMenu 的开头有按钮?

最佳答案

尝试在每个项目之间添加灵活的空间,它可能会解决您的问题并提供一致的 View 。

// 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]];

关于ios - UIToolbar - 添加无间隙的等宽按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com