objective-c - 如何将导航栏的Title View改成一些Custom View并填充原来的尺寸

标签 objective-c uinavigationcontroller uinavigationbar xcode4.5

我想用常规文本字段更改导航栏的标题 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/

相关文章:

ios - 如何以编程方式在 Storyboard 中推送 segue?

ios - 设置导航 Controller 的初始 View Controller

ios - 导航栏和导航按钮中的单独标题

iphone - NSTimer 选择器调用

iphone - 单击 NavigationView 中嵌入的弹出窗口后,如何查看最新的 subview ?

ios - 除非滚动表格,否则 UIView 在 UITableViewCell 内调整大小不会显示

ios - 导航栏标题不显示

swift - 如何显示和隐藏navigationBar?

ios - 为什么在重新启动 iOS 模拟器时我的 plist 变成二进制并丢失数据

ios - 解析字典的问题