淡入/淡出导航控制器标题
我已经使用 title 属性设置了导航控制器的标题。我希望标题能够淡入和淡出以响应用户执行的某些操作。
我当前的解决方案是为导航控制器提供自定义 titleView,并且每当我希望标题淡出时,我都会为该视图的 alpha 变化设置动画。这有效,但产生了另一个问题:我无法居中对齐标题,因为左栏项目和右栏项目的宽度不同。
因此,我正在寻找替代解决方案。是否有另一种方法可以在不使用自定义标题视图的情况下淡化导航控制器标题?
编辑:我收到了一些答案,展示了如何通过添加子视图来做到这一点。尽管这确实启用了我想要的淡入/淡出功能,但我想避免使用子视图,因为我在实现该解决方案时遇到了问题。
导航对象中是否内置了允许标题淡入淡出的内容?我已经做了一些研究,从迄今为止的反应来看,我猜还没有。我只是作为最后的手段发帖,以防万一有人知道一种方法。
I have set my navigation controller's title with the title property. I would like the title to be able to fade in and out in response to some action performed by the user.
My current solution is to provide the navigation controller with a custom titleView, and I would animate the alpha change of that view whenever I want the title to fade. This works, but is creating another problem: I cannot center align the title since the left bar item and the right bar item are varied in width.
Therefore, I am looking for an alternate solution. Is there another way to fade the navigation controller title without using a custom title view?
EDIT: I have received some answers showing how to do this by adding subviews. Although that does enable the fade in/out feature I want, I would like to avoid using subviews as I ran into a problem when implementing that solution.
Is there something built into the navigation objects that allows the fading of the title? I have already done some research, and gauging form the responses thus far, I'm guessing there isn't. I'm just posting as a last resort, just incase anyone knows of a way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
同意 @ImaginaryCake 的评论:调整 title 属性听起来像是黑客。正确的方法是使用 titleView 而不是迭代
UINavitationBar -subviews
并希望它能够在 SDK 更新后继续存在。快速&只需 2 步即可清洁;在 UIViewController 中,执行以下操作:
第 1 步:更改
navigationItem
的titleView
。第 2 步:随意制作动画。
Agreed with @ImaginaryCake comment: Tweaking the title property sounds like a hack. The proper way is to use the titleView instead of iterating through the
UINavitationBar -subviews
and hope it will survive SDK updates.Quick & clean in only 2 steps ; From a UIViewController, do:
Step 1: Change the
titleView
for yournavigationItem
.Step 2: Animate at will.
它可能对你有用......
问候,
CN西瓦库玛
It may be useful for u....
Regards,
CNSivakumar
UIViewController
的title
属性只是一个NSString
。您需要将自定义视图添加到导航栏。The
title
property ofUIViewController
is just aNSString
. You need to add your custom view to your NavigationBar.快速而肮脏:您应该能够通过以下方式将自定义标签添加到导航栏:
只需将标签的框架设置为在导航栏中居中,并像以前一样修改其 alpha 值。然而,这是一个真正的黑客,我不确定当您推送和弹出视图控制器时它的行为如何。
编辑:
好的,给你。首先假设我们有一个导航栏,其中包含具有 title 属性(不是 titleView 属性)的导航项以及左侧和右侧栏按钮项集。
现在您可以像这样淡出标题视图:
我不知道标题视图是否始终位于索引 0,但您可以使用检查您的特定情况
对于标题视图,您将看到类似的内容
Quick and dirty: You should be able to add a custom label to the navigation bar via
Just setup the label's frame to be centered within the navigation bar and modify its alpha value as you did before. However, this is a real hack and I'm not sure how it behaves when you're pushing and popping view controllers.
EDIT:
Ok, here you go. First assume we have a navigation bar holding a navigation item with the title property (not the titleView property) as well as the left and right bar button item set.
Now you can fade the title view like so:
I don't know if the title view is always at index 0, but you can check for your particular case using
For the title view you will see something like