【问题标题】:UIBarButtonItem:setBackgroundVerticalPositionAdjustment not working properly with custom buttonUIBarButtonItem:setBackgroundVerticalPositionAdjustment 无法与自定义按钮一起正常工作
【发布时间】:2023-03-03 01:38:01
【问题描述】:

我有以下代码来自定义 UIBarButtonItem (locationButton) 的显示:

    UIButton *locationButtonAux = [UIButton buttonWithType:UIButtonTypeCustom];
    [locationButtonAux setFrame:CGRectMake(0, 0, LOCATION_BUTTON_WIDTH, LOCATION_BUTTON_HEIGHT)];
    [locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateNormal];
    [locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateHighlighted];
    [locationButtonAux addTarget:self action:@selector(userLocation) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:locationButtonAux];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"title"
                                                              style:UIBarButtonItemStyleDone
                                                             target:nil action:@selector(someMssage)];
    [locationButton setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = locationButton;

我想把这个按钮的位置调整到 NavigationBar 中,因为我的 NavigationBar 比正常高(使用 Appeareance 自定义)。

我正在使用 setBackgroundVerticalPositionAdjustment 方法(如您在代码中所见)来执行此操作。但是我的 UIBarButtonItem 根本不工作,无论我放什么偏移,按钮总是出现在栏的底部附近(快照)。 但是,如果我使用具有普通样式的普通 UIBarButtonItem(我称之为按钮的那个),我可以看到按钮的位置将如何改变 -20 的偏移量。很奇怪...有什么想法吗?

谢谢!

【问题讨论】:

  • 看看stackoverflow.com/a/16442006/1328096,它会为您提供充分的帮助..
  • 一个从不接受答案的提问者(复选标记)不会为人们花时间帮助他提供太多动力。

标签: iphone objective-c uibarbuttonitem uiappearance


【解决方案1】:

也许这样的东西对你有用:

UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)]; //change this frame to counteract for your taller navbar
[rightView addSubview:locationButtonAux];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;

希望对你有帮助

【讨论】:

    【解决方案2】:

    您需要将按钮放在空白视图中并将其放置在该空白视图中。现在将该视图设为栏按钮项的自定义视图。

    这里你真的不需要按钮,因为这只是一个图像并且你没有使用任何按钮功能(你可以附加一个手势识别器来发现点击)。所以这里有一个使用图像视图的简单解决方案:

    UIImageView *locationButtonAux = 
        [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"location_button.png"]];
    UIView* v = [[UIView alloc] initWithFrame:locationButtonAux.frame];
    [v addSubview:locationButtonAux];
    CGRect f = locationButtonAux.frame;
    f.origin.y -= 10;
    locationButtonAux.frame = f;
    UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:v];
    self.navigationItem.rightBarButtonItem = locationButton;
    

    我省略了手势识别器部分,但您可以轻松了解如何添加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多