UINavigationBar 的问题

发布于 2024-11-24 20:11:35 字数 445 浏览 0 评论 0原文

我在这方面有很多问题。

  1. 首先,UINavigationController 内置的 UINavigationBar 将其余 UIViewController 的位置向下移动(我必须手动将位置向上移动)。我在 stackoverflow 上搜索过,一个建议是隐藏当前导航栏并在界面生成器中手动添加 UINavigationBar。这让我想到了第二个问题:
  2. 如果我通过 IB 手动添加 UINavigationBar,导航就会“断开”,例如,需要手动实现后退按钮才能弹出视图控制器。这让我想到了第三个问题:
  3. 我得到的规范表明它需要具有后退按钮形状的后退按钮(按钮的左侧像三角形一样倾斜)。不幸的是,我到处搜索,每个人都说你必须手动添加图像才能使其看起来像后退按钮。但是如果我的后退按钮标题比图像长怎么办?我必须为每个标题制作一个图像!

任何解决其中任何 3 个问题的方法都会非常有帮助!

I'm having so many problems with this.

  1. First of all, the UINavigationBar built-in from the UINavigationController nudges the position of the rest of my UIViewControllers down (which I have to manually nudge the position back up). I've searched on stackoverflow and one suggestion says to hide the current navbar and manually add a UINavigationBar in the interface builder. Which brings me to my second problem:
  2. If I add a UINavigationBar manually through IB, the navigation becomes "disconnected", for example, the back button needs to be implemented manually to pop view controllers. Which brings me to my third problem:
  3. The specification I was given stated that it wants the back button with the shape of a back button (left of the button is slanted like a triangle). Unfortunately I've been searching everywhere and everyone is saying you'll have to manually add an image to make it look like a back button. But what if my back button title is longer than the image? I would have to make an image for each title!

Any solutions to any 3 of those problems would be extremely helpful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2024-12-01 20:11:35

第一个问题:我使用 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;
}

请注意,我' m 使用系统字体,如果您想要不同的字体,请添加另一个参数。然后在控制器的 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;

我们用这段代码实现了什么?尺寸完美的按钮,可与任何本地化字符串配合使用,并且无需为每种支持的语言生成 2x PNG(普通 + 视网膜)。这类似于 UIKit 用于绘制带有标题的按钮的 Core Graphics 代码,因此它不会减慢您的 UI 速度。

First problem: I use a parent view with 415 pixels because 45 (460-415) are taken by the nav bar. No need to nudge since I start with 415.

Second and third: not really. You can use a stretchable image where you draw the text. For this recipe you need a button with a left and right side, and one pixel in the middle which will be stretched to the size of the (left side + right side + length of the text in the middle).

Example: left button with 12 + 1 + (text size) + 5 pixels

left button

Then do this:

/**
 * 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;
}

Note that I'm using the system font, if you want something different, add another parameter. And then you add the following on the viewLoad of your controller:

// 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;

What do we achieve with this code? perfectly sized buttons that work with any localized string, and saves you from generating 2x PNG (normal + retina) for each language supported. This is similar to the Core Graphics code UIKit uses to paint buttons with title so it's not going to slow down your UI.

看春风乍起 2024-12-01 20:11:35

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

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

In XIB, You have to set Navigation Bar style and status bar style of a UIView(in UiViewController.xib) to see the actual layout.

If You want to bring down the navigation bar, then So can adjust the frame of UINavigationController view when you are going to add it on window. bUt here you have to take care of lower part of each UIViewController under UINavigationController stack.

输什么也不输骨气 2024-12-01 20:11:35

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

UIViewController 这称为 wantsFullScreenLayout。对于要添加到导航堆栈的每个视图控制器,将其设置为YES,它们将忽略存在导航栏的事实。只需记住相应地设置子视图的框架即可。

这纠正了你的第一个问题。其余问题的解决方案之后你就会自然而然地找到。

That's a cascade of problems. I have a simple answer, however.

There's a nifty little property of UIViewController that's called wantsFullScreenLayout. Set it to YES for every view controller that you want to add to the navigation stack and they will ignore the fact that there is a navigation bar. Just remember to set the frames of your subviews accordingly.

This corrects your first problem. The solution to the rest should come naturally to you afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文