0

我想以编程方式将三个 UIBarButtonItems 设置为 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];

但我没有得到想要的结果。我的按钮是重叠的并且在左侧并且也采用默认宽度。

4

2 回答 2

1

在这里,我在左侧创建 3 个条形按钮,在右侧创建 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;
于 2013-09-09T07:27:38.210 回答
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];
于 2013-09-09T11:02:55.597 回答