首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UINavigationBar困境

UINavigationBar困境
EN

Stack Overflow用户
提问于 2011-07-18 18:43:52
回答 3查看 463关注 0票数 0

我在这件事上有很多问题。

  1. 首先,UINavigationBar内置于UINavigationController中,它将我的UIViewControllers的其余部分的位置向下推(我必须手动将位置推回原处)。我搜索了堆栈溢出,其中一项建议是隐藏当前的导航条,并在接口构建器中手动添加一个UINavigationBar。这就引出了我的第二个问题:
  2. 如果我通过IB手动添加一个UINavigationBar,导航就变成“断开连接”,例如,需要手动实现back按钮才能弹出视图控制器。这就引出了我的第三个问题:
  3. 我得到的说明是,它想要后退按钮的形状(按钮的左边像一个三角形)。不幸的是,我一直在到处搜索,每个人都说,你必须手动添加一个图像,以使它看起来像一个后退按钮。但是,如果我的后退按钮标题比图像长呢?我将不得不为每个标题做一个图像!

任何解决这些问题的任何三个都是非常有帮助的!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-18 21:11:03

第一个问题:我使用带有415像素的父视图,因为45 (460-415)是由导航条获取的。从415开始就没必要推了。

第二和第三:不完全是。你可以用一个可伸缩的图像来画文字。对于这个菜谱,您需要一个左右两面的按钮,中间有一个像素,它将被拉伸到(左侧+右侧+中间文本的长度)的大小。

示例:左侧按钮,12 +1+(文本大小)+5像素

然后这样做:

代码语言:javascript
复制
/**
 * 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上添加以下内容:

代码语言:javascript
复制
// 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。

票数 1
EN

Stack Overflow用户

发布于 2011-07-18 21:05:38

在XIB中,必须设置UIView的导航栏样式和状态栏样式(在UiViewController.xib中)才能查看实际布局。

如果您想要关闭导航栏,那么当您要将其添加到窗口时,可以调整UINavigationController视图的框架。这里的bUt,您必须在UINavigationController堆栈下处理每个UIViewController的较低部分。

票数 1
EN

Stack Overflow用户

发布于 2011-07-18 20:15:00

这是一连串的问题。不过,我有一个简单的答案。

UIViewController有一个很棒的小属性,叫做wantsFullScreenLayout。将要添加到导航堆栈中的每个视图控制器设置为,YES,,它们将忽略导航栏这一事实。只需记住相应地设置子视图的框架。

这纠正了你的第一个问题。其他问题的解决办法应该会自然而然地出现在你身上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6737732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档