1. 程式人生 > >IOS中圖片作為button的背景圖拉伸

IOS中圖片作為button的背景圖拉伸

適用於IOS6.0    

    UIImage *image2 = [UIImageimageNamed:@"header_left_back_normal"];

    CGFloat top = 0; // 頂端蓋高度

    CGFloat bottom = 0 ; // 底端蓋高度

    CGFloat left = 15; // 左端蓋寬度

    CGFloat right = 15; // 右端蓋寬度

    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);

    image2 = [image2 resizableImageWithCapInsets:insets

resizingMode:UIImageResizingModeTile];

//    UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區域來填充圖片

//    UIImageResizingModeTile:平鋪模式,通過重複顯示UIEdgeInsets指定的矩形區域來填充圖片   

UIButton *backBtn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    backBtn2.frame = CGRectMake(0, 100, screenWidth, 29);

    [backBtn2

setBackgroundImage:image2 forState:UIControlStateNormal];

    [backBtn2 setBackgroundImage:image2 forState:UIControlStateSelected];

    [backBtn2 addTarget:selfaction: @selector(customDefaultBackButtonClick) forControlEvents: UIControlEventTouchUpInside];

    [backBtn2 setTitle:@"討論組"forState:UIControlStateNormal];

    [backBtn2.titleLabel setFont:[UIFont fontWithName:@"Arial" size:13]];

    [self.view addSubview:backBtn2];