Skip to content

Instantly share code, notes, and snippets.

@dsxsxsxs
Forked from brandhill/ninePatchDemo.swift
Created October 8, 2020 02:08
Show Gist options
  • Save dsxsxsxs/544244d86984e3714d4834648776be7a to your computer and use it in GitHub Desktop.
Save dsxsxsxs/544244d86984e3714d4834648776be7a to your computer and use it in GitHub Desktop.
Equivalent Android 9-patch for iOS and Swift 3.0 (multiple stretchable areas)
// origin
UIImageView *unHandleImg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH-200)/2, 100, 200, 30)];
unHandleImg.image = [UIImage imageNamed:@"theImage"];
[self.view addSubview:unHandleImg];
// with stretchable
UIImageView *handleImg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH-200)/2, 200, 200, 30)];
UIImage *img = [UIImage imageNamed:@"theImage"];
// stretchable areas
img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(35, 35, 35, 35) resizingMode:UIImageResizingModeStretch];
handleImg.image = img;
[self.view addSubview:handleImg];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment