在工具栏上设置多个UIBarButtonItem并在它们之间添加空格

时间:2013-09-09 07:18:59

标签: iphone xcode uibarbuttonitem

我想以编程方式将三个UIBarButtonItem设置为UIToolbar,与在线应用程序相同,即 在极左 - 添加按钮 之后--Emo按钮和极右发送按钮

到目前为止,我已经完成了这个

 UIImage *imageSend = [UIImage imageNamed:@"btnSend.png"];
    imageSend          = [imageSend stretchableImageWithLeftCapWidth:floorf(imageSend.size.width/2) topCapHeight:floorf(imageSend.size.height/2)];

    UIButton *btnSend             = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSend.titleLabel.font         = [UIFont boldSystemFontOfSize:15.0f];
    btnSend.titleLabel.shadowOffset = CGSizeMake(0, -1);
    btnSend.titleEdgeInsets         = UIEdgeInsetsMake(0, 2, 0, 2);
    btnSend.contentMode             = UIViewContentModeScaleToFill;

    [btnSend setBackgroundImage:imageSend forState:UIControlStateNormal];
    [btnSend setTitle:@"Send" forState:UIControlStateNormal];
    [btnSend addTarget:self action:@selector(inputButtonPressed) forControlEvents:UIControlEventTouchDown];
    [btnSend sizeToFit];

    self.barBtnSend = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    self.barBtnSend.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.barBtnSend setCustomView:btnSend];


    self.barBtnSend.width=235;
    /* Disable button initially */
    self.barBtnSend.enabled = NO;


    UIImage *imgAttach = [UIImage imageNamed:@"iconAttach.png"];

    self.btnAttach = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnAttach setBackgroundImage:imgAttach forState:UIControlStateNormal];
    [self.btnAttach setFrame:CGRectMake(3, 3, 28, 29)];
    [self.btnAttach addTarget:self action:@selector(inputImageButtonPressed) forControlEvents:UIControlEventTouchDown];


    self.barBtnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self.barBtnAdd setCustomView:self.btnAttach];
    self.barBtnAdd.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    self.barBtnAdd.width=10;


    UIImage *imgEmoji = [UIImage imageNamed:@"iconSmiley.png"];
    imgEmoji = [imgEmoji stretchableImageWithLeftCapWidth:floorf(imgEmoji.size.width/2) topCapHeight:floorf(imgAttach.size.height/2)];
//    
    self.btnCustomEmoji = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnCustomEmoji setBackgroundImage:imgEmoji forState:UIControlStateNormal];
    [self.btnCustomEmoji setFrame:CGRectMake(34, 3, 28, 29)];
    [self.btnCustomEmoji addTarget:self action:@selector(showCustomKeyboard) forControlEvents:UIControlEventTouchDown];
    [self.btnCustomEmoji sizeToFit];
    [self addSubview:self.btnCustomEmoji];

    self.barBtnCustomEmoji = [[UIBarButtonItem alloc] initWithCustomView:self.btnCustomEmoji];
  self.barBtnCustomEmoji.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;


    NSArray *items = [NSArray arrayWithObjects:self.barBtnSend,self.barBtnAdd,nil];
   [self setItems:items animated:NO];

但我没有得到理想的结果。 我的按钮重叠,位于左侧,也采用默认宽度。

2 个答案:

答案 0 :(得分:1)

这里我在左侧创建3个按钮1,在右侧创建2个按钮,但您可以根据需要修改代码并使用。

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPlayerDetailsView)];
UIBarButtonItem *sortButton = [[UIBarButtonItem alloc] initWithTitle:@"Sort Players" style:UIBarButtonItemStylePlain target:self action:@selector(SortPlayersList)];

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:addButton,sortButton, nil];
self.navigationItem.leftBarButtonItem   = self.editButtonItem;

答案 1 :(得分:0)

我通过以下代码解决了自己的问题。

UIImage *imageSend = [UIImage imageNamed:@"btnSend.png"];
imageSend          = [imageSend stretchableImageWithLeftCapWidth:floorf(imageSend.size.width/2)-20 topCapHeight:floorf(imageSend.size.height/2)];

UIButton *btnSend             = [UIButton buttonWithType:UIButtonTypeCustom];
btnSend.titleLabel.font         = [UIFont boldSystemFontOfSize:15.0f];
btnSend.titleLabel.shadowOffset = CGSizeMake(0, -1);
btnSend.titleEdgeInsets         = UIEdgeInsetsMake(0, 2, 0, 2);
btnSend.contentMode             = UIViewContentModeScaleToFill;

[btnSend setBackgroundImage:imageSend forState:UIControlStateNormal];
[btnSend setTitle:@"Send" forState:UIControlStateNormal];
[btnSend addTarget:self action:@selector(inputButtonPressed) forControlEvents:UIControlEventTouchDown];
[btnSend sizeToFit];

self.barBtnSend = [[UIBarButtonItem alloc] initWithCustomView:btnSend];
self.barBtnSend.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

/* Disable button initially */
self.barBtnSend.enabled = NO;

UIImage *imgAttach = [UIImage imageNamed:@"iconAttach.png"];

self.btnAttach = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnAttach setBackgroundImage:imgAttach forState:UIControlStateNormal];
[self.btnAttach setFrame:CGRectMake(0, 3, 28, 29)];
[self.btnAttach addTarget:self action:@selector(inputImageButtonPressed) forControlEvents:UIControlEventTouchDown];

self.barBtnAdd = [[UIBarButtonItem alloc] initWithCustomView:self.btnAttach];
self.barBtnAdd.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
self.barBtnAdd.width=28;
UIImage *imgEmoji = [UIImage imageNamed:@"iconSmiley.png"];

self.btnCustomEmoji = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnCustomEmoji setBackgroundImage:imgEmoji forState:UIControlStateNormal];
[self.btnCustomEmoji setFrame:CGRectMake(30, 3, 28, 89)];
[self.btnCustomEmoji addTarget:self action:@selector(showCustomKeyboard) forControlEvents:UIControlEventTouchDown];
[self.btnCustomEmoji sizeToFit];
self.barBtnCustomEmoji = [[UIBarButtonItem alloc] initWithCustomView:self.btnCustomEmoji];
self.barBtnCustomEmoji.width=28;

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flexItem.width=160;

NSArray *items = [NSArray arrayWithObjects:self.barBtnAdd,self.barBtnCustomEmoji,flexItem, self.barBtnSend,nil];

[self setItems:items animated:NO];