在iOS 10中,我可以这样做来制作背景渐变:
let gradientColor = UIColor.gradientWithFrame(frame: navigationBar.bounds, colors: [.red, .blue])
navigationBar.barTintColor = gradientColor
现在,当navigationBar.bounds
没有大标题时,UINavigationBar
会返回navigationBar.size
的大小。这个屏幕截图显示了渐变重复:
您可以看到渐变开始重复,因为UINavigationBar
返回的大小会返回不正确的大小。
还有另一种方法可以在var userClaims = await _userManager.GetClaimsAsync(user);
设置渐变吗?
答案 0 :(得分:0)
我会将导航栏色调设置为“清除颜色”,然后将渐变添加到背景视图中。
答案 1 :(得分:0)
您需要考虑我的6s设备上的状态栏高度20,您可以使用List<agentDTO> tList = new ArrayList<agentDTO>();
tList = agentCli.findAll_JSON(List.class);
并将此高度添加到框架中。
另外,我通过从CAGradientLayer创建UIImage来设置渐变,因此我可以使用UIApplication.shared.statusBarFrame.height
方法。
UIColor(patternImage:)
答案 2 :(得分:0)
只需将扩展名添加到UIColor
extension UIColor {
static func gradientColor(startColor: UIColor, endColor: UIColor, frame: CGRect) -> UIColor? {
let gradient = CAGradientLayer(layer: frame.size)
gradient.frame = frame
gradient.colors = [startColor.cgColor, endColor.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
UIGraphicsBeginImageContext(frame.size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
gradient.render(in: context)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
return UIColor(patternImage: image)
}
}
执行:
if let navFrame = self.navigationController?.navigationBar.frame {
let newframe = CGRect(origin: .zero, size: CGSize(width: navFrame.width, height: (navFrame.height + UIApplication.shared.statusBarFrame.height) ))
self.navigationController?.navigationBar.barTintColor = UIColor.gradientColor(startColor: .systemPink, endColor: .orange, frame: newframe)
}
并与起点/终点结合以添加水平/垂直渐变
答案 3 :(得分:0)
尝试一下:
let gradientLayer = CAGradientLayer()
var updatedFrame = self.navigationController!.navigationBar.bounds
updatedFrame.size.height += view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
gradientLayer.frame = updatedFrame
gradientLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.navigationController!.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
let appearance = navigationController!.navigationBar.standardAppearance.copy()
appearance.backgroundImage = image
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance