Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Interface Builder(iPhone开发人员)自定义按钮背景图像不考虑拉伸设置_Iphone_Xcode_Uikit_Interface Builder_Uibutton - Fatal编程技术网

Interface Builder(iPhone开发人员)自定义按钮背景图像不考虑拉伸设置

Interface Builder(iPhone开发人员)自定义按钮背景图像不考虑拉伸设置,iphone,xcode,uikit,interface-builder,uibutton,Iphone,Xcode,Uikit,Interface Builder,Uibutton,我正在尝试使用Interface Builder中的背景图像创建自定义按钮。图像具有可拉伸和不可拉伸的部分,因此可以调整其大小 IB公开了拉伸属性以实现这一点,但我输入的值不会影响按钮的显示方式。它总是完全拉伸以填充框架的大小 这是IB或UIButton中不支持的功能吗 (注意:上面的拉伸值不是可以正确处理图像的值,而只是我在截图时弄乱的值)在开始iPhone 3开发的示例中,仅使用UIImage leftCapWidth和topCapHeight,并且图像是以编程方式创建/设置的,因此,如果

我正在尝试使用Interface Builder中的背景图像创建自定义按钮。图像具有可拉伸和不可拉伸的部分,因此可以调整其大小

IB公开了拉伸属性以实现这一点,但我输入的值不会影响按钮的显示方式。它总是完全拉伸以填充框架的大小

这是IB或UIButton中不支持的功能吗


(注意:上面的拉伸值不是可以正确处理图像的值,而只是我在截图时弄乱的值)

在开始iPhone 3开发的示例中,仅使用UIImage leftCapWidth和topCapHeight,并且图像是以编程方式创建/设置的,因此,如果contentStretch不起作用,这是另一种方法。

如果要拉伸多个像素(即按钮中心的图案),则可拉伸图像不起作用

UIButton的contentStretch也不能正常工作

解决方法:我将UIButton子类化,添加了一个
UIImageView*backgroundImageView
作为属性,并将其作为
UIButton
中的子视图放置在索引0处。然后,我在LayoutSubView中确保它完全适合按钮,并设置imageView的高亮显示状态。您只需在应用了正确的contentStretch和contentMode的情况下切换UIImageView

.h文件

@interface ButtonWithBackgroundImage : UIButton {
    UIImageView *backgroundImageView;
}
@property (retain) UIImageView *backgroundImageView;
+ (ButtonWithBackgroundImage*)button;
@end
@implementation ButtonWithBackgroundImage
@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
.m文件

@interface ButtonWithBackgroundImage : UIButton {
    UIImageView *backgroundImageView;
}
@property (retain) UIImageView *backgroundImageView;
+ (ButtonWithBackgroundImage*)button;
@end
@implementation ButtonWithBackgroundImage
@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

我已经让它工作了!!!对我来说,神奇的值是0.2和0.2

我的图像是一个盒子中的一个盒子,两个盒子都有圆角

当该值为0.2和0.8时,图像顶部将保持未拉伸状态,但底部将被拉伸和扭曲

我猜,第一个0.2是偏移量,表示不拉伸多少,第二个可能是拉伸多少。至少,0.2和0.6也起作用


原则上,你可以做一个实验:拍摄一个晶格、同心圆或颜色渐变的图像,然后拉伸它。如果您这样做,请发布图片:)。

按钮的背景图片将始终调整大小以填充框架;前景图像将不会调整大小(如果没有文本,则将其居中)。拉伸的逻辑是在保持边界完整的同时调整中间的大小;例如,对于圆角矩形,这意味着圆角不会被扭曲。

使用Xcode切片功能指定图像可调整大小的中心区域的尺寸,并可以选择指定端盖,端盖是图像中不应由可调整大小的区域填充的区域。

这似乎并不像看上去那样有效。文档中提到的这些值正是我想要的(整个按钮图像拉伸)。考虑到我们有一个完整的GUI来制作GUI,必须通过编程使按钮看起来正确有点奇怪。。。aka IB。查看contentStretch的文档,IB显示的默认值(特别是0.50)不是h/w 0,0和大小1,1的实际默认值,因此我想IB中的设置不起作用也就不足为奇了。看起来contentStretch是在3.0中添加的,所以可能与此有关。我不明白。我没有使用默认值。正如我所说的,我在其中所提出的任何价值观都不会产生任何影响。屏幕截图中显示的不是默认值。我在3.0下运行,这里也有同样的问题。到目前为止,它一直在代码中,但希望它也能在IB中工作……为我保存代码iBuffts。如果任何人都希望看到这个固定或更好的文档,请考虑提交bug(或者至少是DUPUN)。rdar://7706446). UICatalog中的相同示例文件存在相同的问题。有进展吗?你必须用代码设置它吗?