UINavigationBar - 更改 UIBarButtonItem 位置

2024-02-28

我在我的应用程序中使用 UINavigationController 及其栏。 现在我想更改 leftBarButtonItem 和 rightBarButtonItem 的位置。 我希望它们位于不同的 x 和 y 位置,具有自定义的宽度和高度。

但问题是,更改框架不会改变 barButtonItem 的任何内容。我现在不知道为什么,但框架总是会重置为其原始坐标。

这是我在 viewWillAppear 中调用的代码:

UINavigationItem *navItem = [[self.navigationBar items] lastObject];

UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,66,30)];
rightview.backgroundColor = [UIColor blueColor];

//add custom view
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:rightview];
navItem.leftBarButtonItem = search;

视图不是从 0/0 开始,例如 x 位置是 0 之前的 5px。

关于如何解决这个问题有什么建议吗?


这对我的 leftBarButtonItem 有用: 如果您希望 UIBarButtonItem 精确显示在位置 (0, 0),您可以创建一个 UIButton,将其框架设置为 (-5, 0, width, height),假设左侧栏按钮项的偏移量为 (5, 0),并将其放置在您设置为 leftBarButtonItem 的 UIView 中。

#import "UINavigationItem+CSAButtons.h"

@implementation UINavigationItem (CSAButtons)


- (void) makeLeftBarButtonWithTarget:(id)target action:(SEL)action {

    UIImage * imageNormal = [UIImage imageNamed:@"normal.png"]; 
    UIImage * imageHighlighted = [UIImage imageNamed:@"highlighted.png"]; 
    UIImage * imageDisabled = [UIImage imageNamed:@"disabled.png"]; 

    // create your button
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.exclusiveTouch = YES;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [button setBackgroundImage:imageHighlighted forState:UIControlStateHighlighted];
    [button setBackgroundImage:imageDisabled forState:UIControlStateDisabled];

    // set the frame of the button (better to grab actual offset of leftbarbuttonitem instead of magic numbers)
    button.frame = CGRectMake(-5.0, 0.0, imageNormal.size.width, imageNormal.size.height);
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageNormal.size.width, imageNormal.size.height)];
    [view addSubview:button];

    // set the barbuttonitem to be the view
    UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.leftBarButtonItem = barButtonItem;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UINavigationBar - 更改 UIBarButtonItem 位置 的相关文章

随机推荐

  • .htaccess 无法在 amazon ec2 ubuntu 实例中工作

    我有一台来自亚马逊 ec2 服务的服务器 在 Linux Ubuntu 上运行 Ubuntu Server 13 04 64位 我已经安装了apache php和mysql 我在文档根目录 即 var www 中添加了一个 htaccess
  • Antlr4 - 有使用 ParseTree Walker 的简单示例吗?

    Antlr4 有一个新类 ParseTreeWalker 但我该如何使用它呢 我正在寻找一个最小的工作示例 我的语法文件是 gram g4 我想解析文件 program txt 到目前为止 这是我的代码 这假设 ANTLR 已经运行了我的语
  • Pentaho日期格式问题

    我的输入 Excel 工作表的字段具有两种不同类型的值列 格式为 YYYY MM DD 现在 当我将 Excel 工作表添加到 Pentaho 中时 我得到的列以及数据类型在日期格式列中显示了字符串数据类型 你可以在下面看到 之后 我尝试与
  • Elixir - 同一行上有多个表达式 - 在函数定义中使用 do: 语法时出现编译器错误

    在 Elixir 中 多个表达式可以用分号 Elixir 在下面的函数定义中抱怨 defmodule Module2 do def func c n do IO inspect c c n end 有错误 CompileError hell
  • iOS应用程序更新通知

    iOS应用程序更新机制如何运作 如何告诉用户 Appstore 上的应用程序已更新 我是否必须在代码中编写功能来显示更新弹出窗口 还是由 Appstore 处理以向用户显示应用程序已更新的通知 苹果有没有提供这方面的文档 您可以使用Harp
  • “对‘QScriptEngine::QScriptEngine() 的未定义引用”

    鉴于此代码 我得到 对 QScriptEngine QScriptEngine 的未定义引用 include
  • 在 C++ 中获取集合的并集、交集或差集

    我有几个关于如何使用 C 集 std set 的问题 有没有办法获得两个 C 集合的并集 交集或差集 编写自己的函数来做到这一点非常容易 但我想知道是否有内置函数 C 集合可以用作映射中的键吗 Use the set difference
  • 在pyside中设置应用程序名称

    我在 Windows 8 和 Qt 5 下使用 Qt Creator Designer 创建了一个应用程序 它开始如下 class Ui MainWindow object def setupUi self MainWindow MainW
  • 是否可以触发/绑定 jQuery UI 事件? [复制]

    这个问题在这里已经有答案了 可能的重复 jQuery AutoComplete 触发更改事件 https stackoverflow com questions 6431459 jquery autocomplete trigger cha
  • 按钮禁用和启用

    我有一个基于 vb net 的 Windows 应用程序 当单击 GO 按钮时 一堆数据将加载到数据库中 因此 在我的应用程序中 一旦单击 GO 按钮 我只想禁用它 并希望在上传完成后重新启用它 现在 在 btnGo Click 的特定方法
  • 如何在Java字符串中输入引号?

    我想在Java中初始化一个字符串 但是该字符串需要包含引号 例如 ROM 我尝试这样做 String value ROM 但这行不通 我怎样才能包括 s 在字符串中 在 Java 中 您可以使用转义引号 String value ROM
  • 使用 PHP 创建加密的 zip 存档

    我正在寻找一种将 txt 文件加密为 zip 的方法 但采用安全密码保护的方式 我的目标是通过电子邮件将此文件发送给我 而任何人都无法阅读附件的内容 有谁知道一种简单且最重要的是安全的方法来实现这一目标 我可以创建 zip 存档 但我不知道
  • 从命令行激活 gcloud 服务帐号时出错

    我指的是这个网址https cloud google com speech docs getting started https cloud google com speech docs getting started开始使用 google
  • Vue 不是构造函数

    I using webpack 在 chrome 中构建并运行后显示此错误 我不知道如何解决它 我的代码很简单 devDependencies babel core 6 23 1 babel loader 6 3 2 babel plugi
  • 将 XML 注释添加到 LINQ to SQL 设计器生成的类属性

    我使用 Visual Studio 中的 LINQ to SQL 设计器来创建数据库的对象模型 现在 我想向每个生成的属性添加 XML 注释 但我不知道如何在下次刷新 dbml 文件时不删除属性的情况下执行此操作 如何才能做到这一点 我相信
  • 无法从本地主机访问 Django

    这有点奇怪 我无法从本地主机访问 django 但我可以从本地 IP 访问它 python manage py runserver 0 0 0 0 8000 然后当我尝试访问时 我的主机文件 127 0 0 1 lmlicenses wip
  • -bash: ./configure: 没有这样的文件或目录 - MySQL 在 Mac OS X 10.6 上安装

    我正在尝试在 Mac OS X 10 6 上安装 MySQL 下载 MySQL 并使用 tar xzvf mysql 5 1 37 tar gz 解压后 我尝试运行此配置行 configure prefix usr local mysql
  • 使用 popen() 调用 shell 命令?

    当通过 xcode 运行以下代码时 我得到不一致的行为 有时它会正确打印 git 版本 有时它不会打印任何内容 但 shell 命令的返回码始终为 0 关于为什么会这样的任何想法吗 我究竟做错了什么 define BUFFER SIZE 2
  • UIRefreshController 结束动画问题

    当我呼唤self refreshControl endRefreshing 它将 tableView 像它应该的那样恢复到原来的位置 我应该如何对其进行动画处理 以便它能够流畅地返回到原来的位置endRefreshing Try this
  • UINavigationBar - 更改 UIBarButtonItem 位置

    我在我的应用程序中使用 UINavigationController 及其栏 现在我想更改 leftBarButtonItem 和 rightBarButtonItem 的位置 我希望它们位于不同的 x 和 y 位置 具有自定义的宽度和高度