3

通常、 を使用するUINavigationControllerと、左側に矢印のある戻るボタンが表示されます。問題は、私が UINavigationController を使用していないということです。とにかくここに写真があります:

ここに画像の説明を入力

UIBarButtonItemこれまでのところ、左側に矢印のない正方形のレギュラーがあります。

そのような左側に矢印があるボタンを作成し、それを UINavigationBar に追加する方法はありますか? 私も見回しましたが、UIButton タイプ 101 は文書化されておらず、拒否されるようです。受け入れられる解決策が必要です!

4

3 に答える 3

3

ボタンに背景画像を与えて、ネイティブの角度付きボタンのように見せることができます。

testButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 30, 160, 44)];
[testButton setTitle:@"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [[UIImage imageNamed:@"angledButton"]  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
[testButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[testButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

あなたはそれを微調整する必要があるかもしれませんが、それは正しい方向にあると私は信じています.

于 2012-05-26T03:06:45.290 に答える
2

ところで。ナビゲーション コントローラーの [戻る] ボタンとまったく同じボタンを作成する場合は、このコードをご覧ください。

@implementation UIButton (CustomBackButton)

- (UIButton*)configureForBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action
{
// Make the text 6 pixels from above, 8 pixels from the right, and 12 pixels from the left of button's frame.
CGFloat padTRL[3] = {6, 8, 12};

// Text must be put in its own UIView, s.t. it can be positioned to mimic system buttons
UILabel* label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0, -1);
label.text = title;
[label sizeToFit];

UIImage* norm = [[UIImage imageNamed:@"backBarButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIImage* click = [[UIImage imageNamed:@"backBarButtonHover.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
[self setBackgroundImage:norm forState:UIControlStateNormal];
[self setBackgroundImage:click forState:UIControlStateHighlighted];
[self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

// Calculate dimensionss
CGSize labelSize = label.frame.size;
CGFloat controlWidth = labelSize.width+padTRL[1]+padTRL[2];
controlWidth = controlWidth>=norm.size.width?controlWidth:norm.size.width;

// Assemble and size the views
self.frame = CGRectMake(0, 0, controlWidth, 30);
[self addSubview:label];
label.frame = CGRectMake(padTRL[2], padTRL[0], labelSize.width, labelSize.height);
return self
}
@end

画像全体を使用する必要はありません。先のとがった部分だけで十分です..タイトルの長さに応じて画像が引き伸ばされます..

于 2012-11-07T09:17:05.563 に答える