【问题标题】:Strange offset on right bar button item右栏按钮项目上的奇怪偏移
【发布时间】:2014-02-25 08:22:48
【问题描述】:

我在UINavigationControllerrightBarButtonItem 上遇到了一个奇怪的问题。 iOS7 上按钮的边距消失了,所以看起来像这样:

一开始我以为是我UINavigationController相关分类的一些错误,但是我把我所有的自定义代码和分类的所有头文件都去掉了,简单地使用了一个UINavigationController和一个空的视图控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

VCTestViewController *vc = [[[VCTestViewController alloc]init]autorelease];
UINavigationController *nc = [[[UINavigationController alloc]initWithRootViewController:vc]autorelease];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];

VCTestViewController.m -> viewDidLoad

self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"hello" style:UIBarButtonItemStyleBordered target:nil action:nil];

问题依然存在(如上图)。我不知道出了什么问题。你对这个问题有什么想法吗?

【问题讨论】:

标签: ios iphone ios7 uinavigationcontroller uinavigationbar


【解决方案1】:

如果它不起作用,您可以分享您的代码行:

- (UIBarButtonItem *)rightNavBarButton
{
    UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [filterBtn setTitle:@"hello" forState:UIControlStateNormal];
    filterBtn.frame = CGRectMake(0, 0,40,27);
    [filterBtn addTarget:self action:@selector(getFriendsList) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *filterNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:filterBtn];
    return filterNavBarItem;
}

【讨论】:

  • 谢谢,这行得通,但这不是我所期望的。这个问题在我的其他项目中没有出现,并且我已经删除了所有额外的代码,但是这个问题仍然存在。我想弄清楚为什么会这样。
  • 您是否设置了任何导航标题视图?添加背景图片并检查 UIBarButton Item 的坐标
【解决方案2】:

我终于弄清楚发生了什么。

βhargavḯ 给出的链接不是我的问题的解决方案,但它是我的问题的原因。我的一个同事写了一个UINavigationItem的类别来缩小iOS7中的空白。然后他用method swizzling把系统方法setRightBarButtonItem:换成了自己的方法。我没有意识到这一点。

所以我认为如果你用自己的方法交换系统方法,方法调配会非常危险。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2021-05-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多