0

私は3つの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

ここでは、左側に 1 つ、右側に 2 つの 3 つのバーボタンを作成しますが、コードを変更して要件に応じて使用できます。

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 に答える