菜鸟教程小白 发表于 2022-12-12 09:20:21

ios - 在 UINavigationController 中添加 UISegmentedControl 作为 titleView


                                            <p><p>我正在尝试以编程方式将 <code>UISegmentedControl</code> 添加为 <code>UINavigationController</code> 中的 <code>titleView</code>。但它没有出现。经过进一步调查,我发现如果根据 <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationItem_Class/Reference/UINavigationItem.html" rel="noreferrer noopener nofollow">Apple docs</a> 未将 <code>leftBarButtonItem</code> 设置为 <code>nil</code>,则会忽略 <code>titleView</code> 属性。 .所以我将它设置为 <code>nil</code> 但分段控件仍然没有显示!</p>

<p>下面是我的代码。</p>

<pre><code>- (void)viewDidLoad
{
    ;

    self.navigationItem.leftBarButtonItem = nil;
    UISegmentedControl *statFilter = [ initWithItems:];
    ;
    self.navigationItem.titleView = statFilter;
}
</code></pre>

<p>我该怎么办?我在这里错过了什么?</p>

<p>谢谢。</p>

<p><strong>编辑</strong>:</p>

<p>感谢所有回复。但是,它们都不适合我,所以我进行了更深入的研究。这一定是我布置申请的方式。我可能应该早点提到这一点。道歉。</p>

<p>我正在创建的是一个 Storyboard 应用程序。主 Controller 是一个 <code>UITabBarController</code>。单击某个选项卡时,我想显示一个 <code>UITableViewController</code>。由于您无法在 <code>UITableViewController</code> 中显示导航栏,我发现我必须添加一个 <code>UINavigationController</code> 并在其中实现一个 <code>UITableViewController</code>。这就是我的应用的基本外观,</p>

<p> <a href="/image/Vk0m7.png" rel="noreferrer noopener nofollow"><img src="/image/Vk0m7.png" alt="enter image description here"/></a> </p>

<p>仪表板 ViewController 连接到一个继承自 <code>UINavigationController</code> 的类
类(class)。也许这就是问题所在? </p>

<p>因为我刚刚创建了一个小型测试应用程序,所以将 <code>UIViewController</code> 嵌入到 <code>UINavigationController</code> 中,并将我在上面的原始问题中发布的代码写在 <code>UIViewController</code> 的 ViewDidLoad 和分段控件显示得很好。现在我不知道如何在我的主应用程序中做同样的事情,因为没有其他 ViewController 或任何连接到导航 Controller 的东西。</p>

<p>对不起,如果我让事情变得更糟。如果有什么不清楚的地方请评论。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我猜你必须将 UISegmentedControll 设为 UIBarButtonItem:</p>

<p>更新:
此代码使 SegmentedControll 居中:</p>

<pre><code>- (void)viewDidLoad{
    self.title = nil;
    UISegmentedControl *statFilter = [ initWithItems:];
    ;

    NSMutableArray *buttonArray   = [ init];
    UIBarButtonItem *flexibleSpace= [ initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    ;
    initWithCustomView:statFilter]];
    ;

    self.navigationItem.leftBarButtonItems = buttonArray;
    ;
}
</code></pre>

<p>希望对你有帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UINavigationController 中添加 UISegmentedControl 作为 titleView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15199383/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15199383/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UINavigationController 中添加 UISegmentedControl 作为 titleView