我想用常规文本字段更改导航栏的标题 View 。然后我想设置 textField 大小以填充旧的“正常”titleView。
我似乎无法在 Storyboard中执行此操作。将文本字段拖动到导航标题 View 所在的位置不起作用。
所以我在
添加了东西-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PO(self.navigationItem.titleView);
CGRect theFrame= self.navigationItem.titleView.frame;
self.navigationItem.titleView=self.searchBar;
//self.searchBar.frame = theFrame;
while (false);
...
它正在使用一个缓存。 PO 是一个打印对象内容的宏。结果在 viewDidAppear 处,self.navigationItem.titleView 为 null。
因此,虽然我可以显示搜索栏,但我无法让搜索栏“填充”它的空间,因为我不知道空间是什么。
我不想硬编码它,因为你知道, future 事情可能会发生变化。
那我该怎么办?
我曾经看到过代码,您只需向其中添加 subview ,而不是设置 self.navigationItem.titleView 。即使在 viewDidAppear 上,这种方法也存在问题,self.navigationItem.titleView 为 0。
我添加了这些代码:
CGRect theFrame= self.navigationItem.titleView.frame;
CGRect theFrame2 = self.searchBar.frame;
CGRect theFrame3 = self.navigationController.navigationItem.titleView.frame;
而且,我不知道如何nslog结构值,但是,theFrame和theFrame3都是0
最佳答案
您可以在viewWillAppear中尝试此操作:
UIView *customTitleView = [[UIView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor clearColor];
//create your UITextField or UILabel or other view and add as subview of customTitleView
self.navigationItem.titleView = customTitleView;
关于objective-c - 如何将导航栏的Title View改成一些Custom View并填充原来的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792940/