在iOS 7中定位导航栏按钮 [英] Positioning Navigation Bar buttons in iOS 7

查看:76
本文介绍了在iOS 7中定位导航栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航栏按钮的自定义图像。在iOS 6中,如果我们使用按钮设置左栏按钮项,则其x值从10px开始。但是在iOS 7中,如果我们做同样的事情,按钮的x值从20px开始。我们有什么方法可以让它从10px开始,因为iOS 7中的按钮外观不是很好吗?

I am using custom image for navigation bar button.In iOS 6,if we set the left bar button item with button,its x value starts from 10px. But in iOS 7,if we do the same thing,x value of button starts at 20px. Is there any way we shall make it start from 10px as the buttons appearance is not so good with that in iOS 7?

推荐答案

可以使用initWithCustomView:方法初始化UIBarButtonItems。因此,您可以创建一些自定义视图(在您的情况下带有自定义图像的导航栏按钮项)并使用该自定义视图初始化栏按钮项。示例:

UIBarButtonItems can be initialized using initWithCustomView: method. So you can create some custom view (in your case navigation bar button item with custom image) and initialize bar button item with that custom view. Example:

    CustomView *view = [[CustomView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:view];

你可以在initWithImage中设置你想要的任何帧:CustomView的方法:

You can set any frame you want in initWithImage: method of CustomView:

- (id)initWithImage:(UIImage *)image {
    self = [super initWithFrame:CGRectMake(0, 0, 50, 44)];

    CGRect backArrowFrame;


    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        backArrowFrame = CGRectMake(-8, 12, 12.5, 20.5);
    } else {
        backArrowFrame = CGRectMake(-2, 12, 12.5, 20.5);
    }

    UIButton *imView = [[UIButton alloc] initWithFrame:backArrowFrame];
    [imView setContentMode:UIViewContentModeScaleAspectFit];
    [imView setBackgroundImage:image forState:UIControlStateNormal];
    [imView addTarget:target action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:imView];


    return self;
}

通过这种方式可以更改UIBarButtonItem的框架。

In this way it is possible to change frame of UIBarButtonItem.

这篇关于在iOS 7中定位导航栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆