繁体   English   中英

UIBoolBut内的UIBarButtonItem位置

[英]UIBarButtonItem position inside UIToolbar

我在1 UIToolBar中有4个UIBarButtonItem(penButton,textButton,highlightButton和stampButton),我试图让每个按钮垂直排列,现在它们水平排列。

NSBundle *currentBundle = [NSBundle bundleForClass:[self class]];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(5, 20, 200, 200)];
    [toolBar setBackgroundColor:[UIColor clearColor]];

    UIButton *penTool = [UIButton buttonWithType:UIButtonTypeCustom];
    penTool.frame = CGRectMake(0, 0, 30, 30);
    [penTool setImage:[UIImage imageNamed:@"pen-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [penTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    penTool.autoresizingMask = UIViewAutoresizingNone;
    penTool.exclusiveTouch = YES;
    penTool.tag = 1;
    UIBarButtonItem * penButton = [[[UIBarButtonItem alloc] initWithCustomView:penTool] autorelease];

    UIButton *textTool = [UIButton buttonWithType:UIButtonTypeCustom];
    textTool.frame = CGRectMake(0, 0, 30, 30);
    [textTool setImage:[UIImage imageNamed:@"text-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [textTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    textTool.autoresizingMask = UIViewAutoresizingNone;
    textTool.exclusiveTouch = YES;
    textTool.tag = 2;
    UIBarButtonItem * textButton = [[[UIBarButtonItem alloc] initWithCustomView:textTool] autorelease];

    UIButton *highlightTool = [UIButton buttonWithType:UIButtonTypeCustom];
    highlightTool.frame = CGRectMake(0, 0, 30, 30);
    [highlightTool setImage:[UIImage imageNamed:@"squarefill-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [highlightTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    highlightTool.autoresizingMask = UIViewAutoresizingNone;
    highlightTool.exclusiveTouch = YES;
    highlightTool.tag = 3;
    UIBarButtonItem * highlightButton = [[[UIBarButtonItem alloc] initWithCustomView:highlightTool] autorelease];

    UIButton *stampTool = [UIButton buttonWithType:UIButtonTypeCustom];
    stampTool.frame = CGRectMake(8, 8, 30, 30);
    [stampTool setImage:[UIImage imageNamed:@"approved" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [stampTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    stampTool.autoresizingMask = UIViewAutoresizingNone;
    stampTool.exclusiveTouch = YES;
    stampTool.tag = 4;
    UIBarButtonItem * stampButton = [[[UIBarButtonItem alloc] initWithCustomView:stampTool] autorelease];


    NSArray *buttons = [NSArray arrayWithObjects: penButton, textButton, highlightButton, stampButton, nil];

    [toolBar setItems:buttons animated:NO];

    [self.view addSubview:toolBar];

我怎样才能解决这个问题,使它们垂直排列?

我不相信你可以让UIToolbar垂直运行。 它的高度限制为44/32像素。 为了您的目的,最简单的方法是创建自定义UIView,并可能使用自动布局使其行为像工具栏。

只需使用界面编译器即可。 我看到我们可以通过autolayout设置工具栏高度,但是barButtonItem无法设置自动布局。 重要的是:栏按钮自动长大以修复图像大小。 因此,准备好需要大小的图像并将其设置为btn项目。 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM