ios - 背景渐变在横向模式下看不正确

标签 ios swift

当我将设备置于横向模式时,背景设置错误。我如何解决它?我还可以用来了解如何将整个应用程序置于横向模式。

override func viewDidLoad() {
    super.viewDidLoad()
    //BACKGROUND FONDO
    //A linear Gradient Consists of two colours: top colour and bottom colour
    let topColor = UIColor(red: 15.0/255.0, green: 118.0/255.0, blue: 128.0/255.0, alpha: 1.0)
    let bottomColor = UIColor(red: 84.0/255.0, green: 187.0/255.0, blue: 187.0/255.0, alpha: 1.0) 

    //Add the top and bottom colours to an array and setup the location of those two.
    let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
    let gradientLocations: [CGFloat] = [0.0, 1.0]

    //Create a Gradient CA layer
    let gradientLayer: CAGradientLayer = CAGradientLayer()
    gradientLayer.colors = gradientColors
    gradientLayer.locations = gradientLocations

    gradientLayer.frame = self.view.bounds
    self.view.layer.insertSublayer(gradientLayer, atIndex: 0)
}    //FIN BACKGROUND GRADIENT

最佳答案

在您的情况下,渐变是图层,而不是 View 。这意味着它不会在包含图层更改时(例如在旋转期间)调整大小或更改位置。您必须在旋转期间手动更改子层的框架。

关于ios - 背景渐变在横向模式下看不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869036/

相关文章:

ios - 如何修复 iOS 7 中的状态栏重叠问题

ios - UIPageControl 不在 valueChanged 上调用选择器

ios - 如何直接将数组中的所有字符串大写?

ios - 为什么 uiimagePickerController.mediaTypes = [kUTTypeMovie as String] 包括 Live Photos 作为 Bounce 或 Loop Effect 作为 Video?

ios - 将 SKSpriteNode 掩蔽为液体

java - 将多个 LiveData 值映射为一个

ios - scrollView 获取过程

arrays - 具有首字母和末字母优化的过滤器数组

ios - 以编程方式设置 segue 标识符

swift - 如何在 UIActivityViewController 中添加自定义条目