我在这件事上有很多问题。
任何解决这些问题的任何三个都是非常有帮助的!
发布于 2011-07-18 21:11:03
第一个问题:我使用带有415像素的父视图,因为45 (460-415)是由导航条获取的。从415开始就没必要推了。
第二和第三:不完全是。你可以用一个可伸缩的图像来画文字。对于这个菜谱,您需要一个左右两面的按钮,中间有一个像素,它将被拉伸到(左侧+右侧+中间文本的长度)的大小。
示例:左侧按钮,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;我们用这个代码实现了什么?与任何本地化字符串一起工作的完美大小的按钮,可以避免为支持的每种语言生成2xPNG(正常+视网膜)。这类似于代码UIKit用来绘制带有标题的按钮,所以它不会减慢您的UI。
发布于 2011-07-18 21:05:38
在XIB中,必须设置UIView的导航栏样式和状态栏样式(在UiViewController.xib中)才能查看实际布局。
如果您想要关闭导航栏,那么当您要将其添加到窗口时,可以调整UINavigationController视图的框架。这里的bUt,您必须在UINavigationController堆栈下处理每个UIViewController的较低部分。
发布于 2011-07-18 20:15:00
这是一连串的问题。不过,我有一个简单的答案。
UIViewController有一个很棒的小属性,叫做wantsFullScreenLayout。将要添加到导航堆栈中的每个视图控制器设置为,YES,,它们将忽略导航栏这一事实。只需记住相应地设置子视图的框架。
这纠正了你的第一个问题。其他问题的解决办法应该会自然而然地出现在你身上。
https://stackoverflow.com/questions/6737732
复制相似问题