Objective c 尤尼亚维加丁巴尔的不幸
我在这方面有很多问题Objective c 尤尼亚维加丁巴尔的不幸,objective-c,ios,xcode,uinavigationcontroller,uinavigationbar,Objective C,Ios,Xcode,Uinavigationcontroller,Uinavigationbar,我在这方面有很多问题 首先,UINavigationController中内置的UINavigationBar会向下移动其余UIViewControllers的位置(我必须手动将位置向后移动)。我在stackoverflow上搜索过,有人建议隐藏当前导航栏,并在界面生成器中手动添加UINavigationBar。这就引出了我的第二个问题: 如果我通过IB手动添加UINavigationBar,导航将变为“断开连接”,例如,需要手动实现“后退”按钮以弹出视图控制器。这就引出了我的第三个问题: 我得
任何解决这三个问题的方法都会非常有用 这是一连串的问题。不过,我有一个简单的答案 它有一个漂亮的小属性,叫做
wantsFullScreenLayout
。对于要添加到导航堆栈中的每个视图控制器,将其设置为YES,它们将忽略存在导航栏的事实。请记住相应地设置子视图的框架
这纠正了您的第一个问题。其他问题的解决方案应该在以后自然出现。在XIB中,您必须设置UIView的导航栏样式和状态栏样式(在UiViewController.XIB中)才能查看实际布局
如果您想关闭导航栏,那么在将其添加到窗口时,您可以调整UINavigationController视图的框架。但在这里,您必须注意UINavigationController堆栈下每个UIViewController的下部。第一个问题:我使用415像素的父视图,因为导航栏使用45(460-415)。没有必要轻推,因为我从415开始 第二和第三:不是真的。可以在绘制文本的位置使用可拉伸图像。对于这个食谱,你需要一个带有左、右的按钮,中间的一个像素将被拉伸到(左边+右边+文本的中间长度)的大小。p> 示例: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用于绘制带有标题的按钮的核心图形代码,因此它不会降低您的UI速度。请问您为什么不希望导航栏轻推可导航VCs的内容?因为否则,它意味着你希望内容在导航控制器的导航栏下。给我的资产是在全屏背景下裁剪的。我要么手动裁剪图像本身,要么轻推代码中的每个屏幕位置以向上移动。我确实想要导航栏下的内容。它似乎不起作用。我将self.WantFullScreenLayout=YES放在UIViewController的viewDidLoad中,没有任何更改。实际上,默认情况下,
WantFullScreenLayout
对导航栏没有任何作用,因为任何视图都应该重叠它们。我主要在需要状态栏下的空间时使用此属性(这样我可以获得视图的完整全屏布局)。您在导航栏中遇到了一些奇怪的行为(当使用IB时,这种情况往往会发生)。我希望这处房产能解决这个问题。顺便问一下,你是如何设置窗口的框架和添加导航控制器的?我会给你一个机会,然后再报告。谢谢