我有一个 leftBarButtonItem
(使用自定义 View )作为后退按钮的替代品(因为自定义 View 不能用于 backBarButtonItem
)。我还有一个 rightBarButtonItem
(也使用自定义 View )和一个自定义 titleView。
当 leftBarButtonItem
小于特定宽度时,一切正常。 rightBarButtonItem
可以很大,titleView 将调整大小并截断以适应可用空间,这正是它应该的样子,也是我想要的样子。
然而,在相同的尺寸反转(rightBarButtonItem
小于特定宽度和 leftBarButtonItem
巨大)时,leftBarButtonItem
完全消失,并且titleView
会拉伸(stretch)以适应以前容纳 leftBarButtonItem
和 titleView
的整个空间。
我尝试交换自定义 View 以查看它是否是我所做的,但同样的问题仍然存在于交换的 View 中,同样仅在 leftBarButtonItem
上。我还尝试使用常规标题,但没有自定义 titleView
,但无济于事。
在 UINavigationItem
的文档中,在 leftBarButtonItems
下,它指出“如果没有足够的空间来显示数组中的所有项目,那些将重叠的项目标题 View (如果存在)或栏右侧的按钮不显示。”但是,应该有足够的空间来容纳按钮和标题,如拥有巨大的 rightBarButtonItem
和截断的标题所示。更不用说,只有一个 leftBarButtonItem
,而不是多个,我需要它来替换后退按钮功能。
关于如何阻止这种情况发生的任何想法?这是我正在使用的代码片段:
self.navigationItem.titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
self.navigationItem.titleView.backgroundColor=[UIColor clearColor];
FollowButton *follow = [[FollowButton alloc] initWithFrame:CGRectMake(0, 0, 62, 34)];
UIBarButtonItem *followButton = [[UIBarButtonItem alloc] initWithCustomView:follow];
self.navigationItem.rightBarButtonItem = followButton;
BackButton *back = [[BackButton alloc] initWithFrame:CGRectMake(0, 0, 108, 34)];
[back addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.leftBarButtonItem = backButton;
最佳答案
使BackButton
的抗压缩优先级更高
关于objective-c - 当 navigationItem.titleView 和 rightBarButtonItem 占据大部分 UINavigationBar 时,如何阻止 leftBarButtonItem 消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14204312/