IOS中图片作为button的背景图拉伸

时间:2023-02-09 14:10:30

适用于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:insetsresizingMode:UIImageResizingModeTile];

//    UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片

//    UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片   

    

    UIButton *backBtn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

    [backBtn2 setBackgroundImage:image2forState:UIControlStateNormal];

    [backBtn2 setBackgroundImage:image2forState:UIControlStateSelected];

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

    [backBtn2 setTitle:@"讨论组"forState:UIControlStateNormal];

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

    

    [self.view addSubview:backBtn2];