- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想自定义我的导航栏,并在导航栏的标题 View 中插入一个带有两个 subview 的 UIView。不幸的是, subview 没有显示。你知道为什么吗?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
问题和预期的解决方案:
最佳答案
为什么将所有元素的高度
用作UIScreen.main.bounds.size.height
?请尝试以下代码。
附言我已经更改了 searchIcon
的颜色,因为我没有图像,并且更改了标签文本的颜色并添加了一些文本以使其可见。
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
以上代码截图如下:
请根据您的要求相应地更改 x,y。
关于ios - Swift 将带有 subview 的 UIView 添加到 titleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49795351/
我已经在我的 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 将其制成一个圆圈层。 圆的上半部
我是一名优秀的程序员,十分优秀!