UIImageView xib裡面拉伸圖片技巧
拉伸圖片的時候程式碼裡和xib裡面的圖片名字去掉@2x,但是原始圖片檔案得要[email protected]
The X and Y values seem to be the positions for the stretch starting point, relative to the entire width and height of the image, i.e. 0.5 would mean a point in the middle of the image.
Same thing for the Width and Height: sizes for the stretchable area, relative to the image size, i.e. setting it to a value of 1 / imageWidth would mean the stretchable area is 1 pixel wide. The strange thing is 0 also work fine for the blue pill button (could be for convenience reasons?).
In cases where you want more than one pixel to stretch (ie. a pattern in the center of the button) the stretchable UIImage does not work.
The UIButton's contentStretch does also not work properly.
How I solved it: I subclassed UIButton and added a UIImageView
*backgroundImageView
as property and placed it at index 0 as a subview within the UIButton
.h file
@interfaceButtonWithBackgroundImage:UIButton{UIImageView*backgroundImageView ;}@property(retain)UIImageView*backgroundImageView;+(ButtonWithBackgroundImage*)button;@end
.m file
@implementationButtonWithBackgroundImage@synthesize backgroundImageView;+(ButtonWithBackgroundImage*)button {return[self buttonWithType:UIButtonTypeCustom];}-(void)setBackgroundImageView:(UIImageView*)img {[backgroundImageView removeFromSuperview];[backgroundImageView release];
backgroundImageView =[img retain];if(backgroundImageView){[self insertSubview:backgroundImageView atIndex:0];[self setNeedsLayout];}}-(void)setSelected:(BOOL)select{[super setSelected:select];// we subclass the setSelect method to highlight the background imageview[backgroundImageView setHighlighted:select||self.highlighted];}-(void)setHighlighted:(BOOL)highl {[super setHighlighted:highl];// we subclass the setHighlighted method to highlight the background imageview [backgroundImageView setHighlighted:highl||self.selected];}-(void)layoutSubviews {[super layoutSubviews];self.backgroundImageView.frame =self.bounds;}-(void)dealloc {[backgroundImageView release];
backgroundImageView =nil;[super dealloc];}@end