菜鸟教程小白 发表于 2022-12-11 17:25:59

iOS:从 AppDelegate 添加 subview 未加载


                                            <p><p>我正在尝试从 AppDelegate 添加一个 subview ,因为我想在所有 subviewController 中显示该 subview 。</p>

<p>我已将它作为导航 Controller 添加到我的 Storyboard中。</p>

<p>这是我的代码:</p>

<pre><code>var navBar = UINavigationController()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {
    self.addViewToController()
    return true
}

func addViewToController(){
    self.navBar = (window?.rootViewController as? UINavigationController)!
    let frame = CGRectMake(0, navBar.navigationBar.frame.size.height + 20, navBar.navigationBar.frame.size.width, 30)
    let newView = UIView()
    newView.frame = frame
    newView.backgroundColor = UIColor.cyanColor()
   window?.addSubview(newView)   
}
</code></pre>

<p>代码已执行,但从未添加 subview 。你们中的任何人都知道为什么或我做错了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我尝试更改一行,以下更新对我有用。</p>

<p>在 <code>application didFinishLaunching</code> 方法中重新键入最后一行代码</p>

<pre><code> window?.rootViewController?.view.addSubview(newView)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS:从 AppDelegate 添加 subview 未加载,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39299639/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39299639/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS:从 AppDelegate 添加 subview 未加载