Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Dshosev/0b9ac14d9aaadab73fdab62ff87abe2c to your computer and use it in GitHub Desktop.

Select an option

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
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