菜鸟教程小白 发表于 2022-12-11 18:56:39

ios - 如何在 Swift 4 中检测我的设备是否是 iPhone?


                                            <p><div>
            <aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
      <div class="d-flex fd-column fw-nowrap">
            <div class="d-flex fw-nowrap">
                <div class="flex--item wmn0 fl1 lh-lg">
                  <div class="flex--item fl1 lh-lg">
                        <b>这个问题在这里已经有了答案</b>:
                        
                  <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您需要检测设备是否为 iPhoneX,请不要使用 <code>bounds</code>,这取决于设备的方向。因此,如果用户以纵向模式打开您的应用程序,它将失败。您可以使用不会在旋转时更改的设备属性 <code>nativeBounds</code>。</p>

<blockquote>
<p>In iOS 8 and later, a screen’s bounds property takes the interface
orientation of the screen into account. This behavior means that the
bounds for a device in a portrait orientation may not be the same as
the bounds for the device in a landscape orientation. Apps that rely
on the screen dimensions can use the object in the
fixedCoordinateSpace property as a fixed point of reference for any
calculations they must make. (Prior to iOS 8, a screen’s bounds
rectangle always reflected the screen dimensions relative to a
portrait-up orientation. Rotating the device to a landscape or
upside-down orientation did not change the bounds.)</p>
</blockquote>

<pre><code>extension UIDevice {
    var iPhoneX: Bool {
      return UIScreen.main.nativeBounds.height == 2436
    }
}
</code></pre>

<hr/>

<p>用法</p>

<pre><code>if UIDevice.current.iPhoneX {
    print(&#34;This device is a iPhoneX&#34;)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 4 中检测我的设备是否是 iPhone?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46633487/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46633487/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 4 中检测我的设备是否是 iPhone?