第1の問題:私は415ピクセルの親ビューを使用します。これは、45(460-415)がナビゲーションバーによって取得されるためです。私が415から始まって以来、軽く振る舞う必要はありません。
2番目と3番目:本当にありません。テキストを描画する場所にストレッチング可能なイメージを使用できます。このレシピでは、左右のボタンと中間の1ピクセルが必要です(中央のテキストの左側+右側+長さ)。
例:12 + 1 +(文字サイズ)+ 5画素

と左ボタンは、この操作を行います。私は、システムフォントを使用してい
/**
* Return the given image with white text written in the middle.
*
* The image should be stretchable.
* The text is written with bold system font 12.
*
* @param btnLeft The original image.
* @param capWidth The capWidth to stretch it.
* @param text The text to draw in the image.
* @return A image containing the original stretched image with some text written in the middle.
*/
+(UIImage*) drawText:(NSString*)text inImage:(UIImage*)image capWidth:(int)capWidth {
if (image==nil) {
return nil;
}
// make it stretchable
UIImage *stretchable = [image stretchableImageWithLeftCapWidth: capWidth
topCapHeight: 0];
UIFont *font = [UIFont boldSystemFontOfSize:12];
// the combined size is that of the image plus the text
CGSize textSize = [text sizeWithFont:font];
CGSize combinedSize = CGSizeMake(image.size.width + textSize.width, image.size.height); // should be +4 for more breath
UIGraphicsBeginImageContext(combinedSize);
// draw the image
[stretchable drawInRect:CGRectMake(0,0,combinedSize.width,combinedSize.height)];
// draw the text
float y = (image.size.height-textSize.height)/2; // leaves even distance to top and bottom
CGRect rect = CGRectMake(capWidth, y, textSize.width,textSize.height); // should be +2 for more breath space
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) // positioning on a fractional point produces blurry text
withFont:font];
// gets the result
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
注、別のものが必要な場合は、別のパラメータを追加します。コントローラのviewLoadに次のコードを追加します。
// buttonLeft is the button UIImage
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [ImageUtils drawText:@"Back" inImage:buttonLeft capWidth:14];
[back setBackgroundImage:image forState:UIControlStateNormal];
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
back.frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIBarButtonItem *backbi = [[[UIBarButtonItem alloc] initWithCustomView:back] autorelease];
self.navigationItem.leftBarButtonItem = backbi;
このコードではどのようなことが達成できますか?ローカライズされた文字列で動作する完璧なサイズのボタンで、サポートされている言語ごとに2倍のPNG(標準+網膜)を生成することを防ぎます。これは、UIKitがタイトル付きのボタンをペイントするために使用するCore Graphicsコードと似ていますので、UIが遅くなることはありません。
なぜナビゲート可能なVCのコンテンツをナビゲーションバーでナッジダウンしたくないのですか?それ以外の場合は、コンテンツがナビゲーションコントローラのナビゲーションバーの下にあることを意味します。 – Eimantas
私に与えられた資産はフルスクリーンの背景を念頭に置いて切り取られました。私は手動で画像自体を切り捨てるか、上に移動するためにコードの各画面の位置を微調整する必要があります。私は、ナビゲーションバーの下にコンテンツが欲しいです。 – ninjaneer