Forked from shiv19/gradientActionbarIosNativeScript.js
Created
January 29, 2020 13:13
-
-
Save Dshosev/0b9ac14d9aaadab73fdab62ff87abe2c to your computer and use it in GitHub Desktop.
use this code with loaded event of a page to get gradient actionbar on iOS for NativeScript Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Frame } from 'tns-core-modules/ui/frame'; | |
| import { Color } from 'tns-core-modules/color'; | |
| import { isIOS } from 'tns-core-modules/platform'; | |
| declare var CAGradientLayer, CGPointMake, UIGraphicsBeginImageContext, UIGraphicsGetCurrentContext; | |
| declare var UIGraphicsGetImageFromCurrentImageContext, UIBarMetrics, CGSizeMake; | |
| export function onActionBarLoaded(args) { | |
| if (isIOS) { | |
| const navigationBar = Frame.topmost().ios.controller.navigationBar; | |
| const gradient = CAGradientLayer.layer(); | |
| const bounds = navigationBar.bounds; | |
| gradient.frame = bounds; | |
| gradient.colors = [new Color('#007fbe').ios.CGColor, new Color('#00bdda').ios.CGColor]; | |
| gradient.startPoint = CGPointMake(0, 0); | |
| gradient.endPoint = CGPointMake(1, 0); | |
| const size = CGSizeMake(bounds.size.width, bounds.size.height); | |
| UIGraphicsBeginImageContext(size); | |
| gradient.renderInContext(UIGraphicsGetCurrentContext()); | |
| const gradientImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| // doesn't work without this setTimeout | |
| setTimeout(() => { | |
| navigationBar.setBackgroundImageForBarMetrics(gradientImage, UIBarMetrics.default); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment