【问题标题】:How to set image for UIBarButtonItem Flexible space - IOS如何为 UIBarButtonItem 灵活空间设置图像 - IOS
【发布时间】:2013-05-06 10:28:36
【问题描述】:

看看这张图片...

我想设计这样的工具栏,每个 UIBarButtonItem 应该用细线分隔。所以我在每个 UIBarButtonItem 之间插入了灵活的空间按钮(这有助于我在方向改变时对齐 UIBarButtonItem),但是如果我为此设置一个图像,它不再充当灵活按钮,而是变为普通的 UIBarbutton。

所以我需要一个带有图像的灵活空间按钮。知道怎么做。 (要么) 如何设计像上面这样的 UIToolbar ......任何帮助都非常感谢。谢谢

【问题讨论】:

    标签: iphone ios cocoa-touch uibarbuttonitem uitoolbar


    【解决方案1】:

    为 UIBarbuttonItem 设置背景图片

    UIImage *faceImage = [UIImage imageNamed:@"facebook.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
    

    【讨论】:

    • 如果是普通的UIBarButtonItem会起作用,对于UIBarButtonItem-flexible space button不起作用。
    【解决方案2】:

    我想这会对你有所帮助..

    UIImage* topImage = [UIImage imageNamed:@"TabBarGradient.png"];
    
    // Create a new image context
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, topImage.size.height*2), NO, 0.0);
    
    // Create a stretchable image for the top of the background and draw it
    UIImage* stretchedTopImage = [topImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
    [stretchedTopImage drawInRect:CGRectMake(0, 0, width, topImage.size.height)];
    
    // Draw a solid black color for the bottom of the background
    [[UIColor blackColor] set];
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, topImage.size.height, width, topImage.size.height));
    
    // Generate a new image
    UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImage;
    

    更多详细信息,您可以查看...

    http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/

    如果没有解决您的问题..请告诉我..

    【讨论】:

    • 好的,我会尽力告诉你的。
    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    相关资源
    最近更新 更多