菜鸟教程小白 发表于 2022-12-12 13:15:35

iphone - 如何将标题标签调整为导航栏的全宽


                                            <p><p>我使用下面的代码在导航栏中有一个标题标签。我似乎无法将其调整到完整 View 的宽度。知道如何解决这个问题吗?</p>

<p>我认为 <code>self.view.bounds.size.width</code> 会将其调整为 View 的整个宽度。</p>

<p>这是我的导航栏标题标签代码,还有一个屏幕截图可以直观地显示问题。</p>

<p>感谢您的帮助</p>

<pre><code> UILabel* tlabel=[ initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @&#34;someText&#34;;
tlabel.font = ;
tlabel.textColor=;
tlabel.backgroundColor = ;
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView=tlabel;
</code></pre>

<p> <img src="/image/XhO5b.png" alt="enter image description here"/> </p>

<p>从 Paras Joshi 更改代码后,右侧是正确的,但左侧是关闭的。</p>

<p> <img src="/image/3VYig.png" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用下面的代码,我在 <code>UIView</code> 中添加了 <code>UILabel</code>,然后将该 View 设置为 titleView... </p>

<pre><code>UIView *headerView = [ initWithFrame:CGRectMake(0, 0, 320, 48)];
UILabel* tlabel=[ initWithFrame:CGRectMake(0,0, 320, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @&#34;someText&#34;;
tlabel.font = ;
tlabel.textColor=;
tlabel.backgroundColor = ;
tlabel.textAlignment = UITextAlignmentCenter;   
;
self.navigationItem.titleView = headerView;
</code></pre>

<p>或添加为如下所示的 subview 。</p>

<pre><code>;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何将标题标签调整为导航栏的全宽,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18014727/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18014727/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何将标题标签调整为导航栏的全宽