How to create a UIImageView with a stretch image

In my application, one requirement is to set the background of the label on an image. I did this by setting the image at the end of UIlabel using this code

.H file

#import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIImageView *lblBGIV; } @end 

.M file

 - (void)viewDidLoad { [super viewDidLoad]; lblBGIV = [[UIImageView alloc] init]; UIImage *lblBGImg = [[UIImage imageNamed:@"textlblbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)]; lblBGIV.image = lblBGImg; } - (void)updateLabelMethod:(NSTimer *)theTimer { lblBGIV.frame = CGRectMake(label.frame.origin.x-9, label.frame.origin.y-5, label.frame.size.width+18, 26); [self.myScrollView addSubview:lblBGIV]; [self.myScrollView sendSubviewToBack:lblBGIV]; } 
+4
source share
2 answers

You need to create another code to set the image for different iOS. use this code

 UIImage *lblBGImg; float currentVersion = 6.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) { //device have iOS 6 or above lblBGImg = [[UIImage imageNamed:@"textlblbg41.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15) resizingMode:UIImageResizingModeStretch]; }else{ //device have iOS 5.1 or belove lblBGImg = [[UIImage imageNamed: @"textlblbg41.png"] stretchableImageWithLeftCapWidth:15.0 topCapHeight:15.0]; } lblBGIV.image = lblBGImg; 

This will do the trick for both iOS.

+8
source

Use resizableImageWithCapInsets:resizingMode: instead.

 UIImage *lblBGImg = [[UIImage imageNamed:@"textlblbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8) resizingMode:UIImageResizingModeStretch]; 
+5
source

Source: https://habr.com/ru/post/1493240/


All Articles