- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了提供一些背景信息,我正在制作一个基于 UINavigationControlled 的博客类型应用程序(我认为它最类似于 iPhone 的 facebook 应用程序)。无论当前处于事件状态的 View 如何,导航栏都会在其标题区域中显示一些按钮,例如“好友请求”和“事件通知”。与 Facebook 应用程序大致相同,单击这些按钮将创建一个弹出 View 。
目前,我正在以一种我认为效率极低的方式做事。对于加载的每个 View ,我都会重新创建按钮并将它们添加到导航栏。我的代码如下:
//Setup the custom middle buttons
UIView *container = [[UIView alloc] init];
container.frame = CGRectMake(0, 0, 80, 44);
// create a button and add it to the container
UIButton *notificationButton = [UIButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 35, 44);
[notificationButton addTarget:self
action:@selector(showNotifications:)
forControlEvents:UIControlEventTouchUpInside];
[container addSubview:notificationButton];
// add another button to the container
UIButton *friendActivityButton = [UIButton buttonWithType:UIButtonTypeCustom];
friendActivityButton.frame = CGRectMake(45, 0, 35, 44);
[friendActivityButton addTarget:self
action:@selector(showFriendActivity:)
forControlEvents:UIControlEventTouchUpInside];
[container addSubview:friendActivityButton];
// Set the titleView to the container view
[self.navigationItem setTitleView:container];
[container release];
由于我的应用程序有多个 View 并且导航栏始终可见,因此继续重新创建按钮,将它们添加到容器 View ,然后将该 View 添加到导航 Controller 的 titleView 似乎很愚蠢。
达到相同效果的更好方法是什么?我正在研究子类化或为 UINavigationBar 创建一个类别,并可能在其中添加容器 View 代码。但是,我不知道如何使选择器在这些情况下工作。我也不确定如何使用 UINavigationBar 类别访问 titleView 属性。
对此的任何帮助都会很棒!谢谢!
最佳答案
我建议将上述代码放入 UIViewController
类别中(如果您可以从单个根对所有 View Controller 进行子类化,则将其放入 UIViewController
子类中)。然后在 viewDidAppear
中,只需调用上面的代码(或将其放在父类(super class)的 viewDidAppear
中)。我认为子类化 UINavigationBar
会带来更多麻烦,而不是其值(value)。
如果您确实想避免重新创建按钮(可能没有理由这样做,但如果出于某种原因这对您很重要),您可以将按钮连接到某个单例对象,而不是将它们指向 self
。单例可以直接处理响应,将消息转发到事件 View Controller ,或者发布事件 View Controller 可以监听的通知。
关于iphone - 向NavigationBar的titleview添加按钮,无需重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004525/
我已经在我的 navigation.titleView 中添加了一个搜索栏 self.navigationItem.titleView = searchBar 还有一个带有 title = "
我正在尝试使用以下代码在 UINavigationItem 上添加我的图像: override func viewDidLoad() { super.viewDidLoad()
Xamarin 新手,我正在尝试在我的跨平台应用程序中创建自定义导航栏。经过一些阅读,我发现我可以在我的 xaml 页面文件中使用标题 View 组件,如下所示。
我正在使用带有自定义 UIView 子类的导航,该子类成为我的 titleView。我想确保这是完整的可用宽度。 从我的 UIViewController 的 viewWillAppear: 逻辑上来
我发现有关使用 UINavigationItem#titleView 和自定义字体的信息很少。当我这样做时,导航栏中的字体垂直不对齐。 这篇文章的部分目的是记录黑客行为,也希望有人对这个问题有一个简洁
我正在尝试添加自定义控件作为 UINavigationBar 中的 titleView。当我这样做时,尽管设置了通常采用全宽的框架和属性,但我得到了: 亮蓝色可以忽略,因为它是我隐藏自定义控件的地方。
我正在尝试动态设置导航栏的文本,以便标题文本始终适合。我是这样完成的: // Pet's Day text "Joy's Day" if let range = currentPet.range(of
我想使用 UITextField 创建一个搜索栏。我尝试将 NavigationItem 的 TitleView 设置为文本字段并设置相应的约束。但导航栏中央的文本字段仍然很短。如何让文本框填满整个导
我正在学习一个 coursera 类(class),我真的到了我认为我不适合这个工作领域(开发)并且应该坚持图形设计的地步。至少我想完成这门类(class),但我不断收到我不明白的错误!根据 logc
我想在第一个 View Controller 的导航栏中添加一个 Logo ,但它的缩放效果很差,因为我不知道最大尺寸是多少。有谁知道吗? 顺便说一句,我知道创建全尺寸图像的技巧,但我不想这样做,我只
当方向改变时调整 UINavigationBar titleview 内容图像大小的最佳方法是什么。我有一张图像高度为 44 像素,另一张图像高度为 32 像素,在旋转 View 后,我更改标题 Vi
在自定义类 MMTimerButton 中,我将按钮设置为响应单击和双击 - (id)initWithFrame:(CGRect)frame { self = [super initWithF
我正在使用 Storyboard ,并将 UIView 添加到带有 2 个子 UILabels 的导航项。我希望 UIView 尽可能达到最大宽度。我注意到我无法使用自动布局来创建约束来执行此操作。这
我的目标是在导航 Controller 页面标题中使用不同字体大小的标题和副标题(标题应该更大,副标题应该相应地更低)。 我找到了实现此功能的代码示例。唯一的问题是未应用字体大小——标题和副标题的字体
我的应用程序适合放在 UINavigationController 中。简单地说,我将 MyUIViewController 设置为 navigationController 的 rootViewCo
我正在尝试推送一个 View Controller 并设置其导航栏的标题,但我猜是因为使用了很长的标题,所以它没有居中。与此同时,后退按钮的范围增加到标题 View ,即如果我点击 Milestone
我想用图像和文本设置 navigationItem 的 titleView,如下图所示: 我该如何实现? 更具体地说,我正在尝试在 Swift 中执行此操作。 最佳答案 条件,你的 Controlle
我有这样的东西: 但我需要为 titleView 设置的 View 在左侧和右侧没有这些边距。像这样: 这就是我实际做的: @property (nonatomic, strong) XHScroll
我在 UIViewController 扩展中使用这个函数来添加一个调整字体以适应宽度的标题。 extesion UIViewController { func setTitleDiffere
在我的 View Controller 中,我将 titleView 设置为 UIView,其中包含一个 UIImageView,使用 setCornerRadius 将其制成一个圆圈层。 圆的上半部
我是一名优秀的程序员,十分优秀!