2016-06-07 64 views
1

我有4 UIBarButtonItem(penButton,textButton,highlightButton & stampButton)裏面1 UIToolBar,我試圖讓每個按鈕垂直排隊,現在他們排隊水平。UIToolbar內UIBarButtonItem位置

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

我該如何解決這個問題,讓他們垂直排隊?

+0

所以你試着做一個垂直工具欄? – CZ54

回答

0

我不相信你可以讓UIToolbar垂直操作。它的高度限制在44/32像素。爲了您的目的,最簡單的方法是創建一個自定義的UIView,並可能使用自動佈局來使其表現得像一個工具欄。

0

只是玩弄interfacebuilder。我看到我們可以通過自動佈局設置工具欄高度,但barButtonItem無法設置自動佈局。 重要的事情:酒吧按鈕自動長大到修復圖像大小。 所以準備你的圖片與需要大小,並將其設置爲btn項目。 enter image description here