简体   繁体   English

使用contentStretch拉伸UIView的一部分

[英]Stretching a portion of a UIView using contentStretch

I need some help understanding the use of the contentStretch property on a UIView. 我需要一些帮助来理解UIView上contentStretch属性的用法。 I'd appreciate 1) some guidance on how to achieve the effect I'm going for and 2) a reference to any comprehensive documentation that exists on how to use this property. 我将不胜感激:1)有关如何实现所需效果的一些指导,以及2)对有关如何使用此属性的任何综合文档的引用。 While I thought I understood the Apple documentation, I now see that I don't. 虽然我以为我了解Apple文档,但现在我不明白。 Here is what I'm trying to do. 这就是我想要做的。

I have a custom UIView that renders a view that looks like figure a. 我有一个自定义的UIView,它呈现了一个如图a所示的视图。 below. 下面。 The drawRect method of this UIView paints the inner box on the left (with the rounded corners and white background) with a different color and affect than is used for the rest of the view. 此UIView的drawRect方法使用与其余视图不同的颜色和效果来绘制左侧的内部框(带有圆角和白色背景)。 Also, the 4 corners of the UIView are not stretchable (this would result in distortion) so I need to stretch only the center of the UIView background. 另外,UIView的4个角不可拉伸(这会导致变形),因此我只需要拉伸UIView背景的中心。

I want to grow this view to a shape similar to that pictured in figure b. 我想将此视图扩大为类似于图b所示的形状。 below. 下面。 In doing so I want only the portion of the view outside the white box (or to the right of the white box) to be stretched/repeated as I increase the UIView's height. 这样做时,我只希望在增加UIView的高度时拉伸/重复白框外(或白框右边)的视图部分。

sketch1

In all of my attempts I have only been able to produce a strecth that affects the entire width of the view and results in a view similar to figure c. 在所有尝试中,我只能产生一个会影响视图整个宽度并导致类似于图c的视图的强度。

sketch2

Is it possible for me to achieve the effect that I want (figure b.) using contentStretch? 我是否可以使用contentStretch达到我想要的效果(图b。)? Is there some other technique I should be using short of re-drawing the entire view, or is re-drawing the only (or best) way to go about this? 除了重新绘制整个视图之外,我还应该使用其他方法吗?或者重新绘制是实现此目的的唯一(或最佳)方法?

Thanks, 谢谢,

To make this happen with contentStretch , you would have to set the contentStretch rectangle so that its top edge is below the bottom edge of the white box, and so that its left edge is to the right of the right edge of the white box. 要做到这一点与contentStretch ,你就必须设置contentStretch矩形,这样它的边是白框的底部边缘下方 ,所以它的左边是白框右侧边缘的右侧 You will probably find that your background pattern gets stretched too much and looks ugly. 您可能会发现您的背景图案拉伸得太多并且看上去很丑。

Instead, give your view a subview that draws just the white box. 而是给您的视图一个仅绘制白色框的子视图。 Set the subview's springs and struts so that the box stays at the upper left and doesn't stretch. 设置子视图的弹簧和支柱,以使该框保持在左上方并且不会拉伸。

Since content stretch is deprecated, best approach is to use auto layout. 由于不建议使用内容扩展,因此最好的方法是使用自动布局。 In the inner rectangle give a defining height and width constant and bind its top and leading constraints to the parent view. 在内部矩形中,提供一个定义的高度和宽度常数,并将其顶部约束和前导约束绑定到父视图。 In order to get a bottom growth in outer rectangle, bind its bottom constraint or alternatively you can also chnage it height. 为了在外部矩形中获得底部增长,请绑定其底部约束,或者也可以更改其高度。

I would make the white box stretchable on top, and a background view filled with a pattern using colorWithPatternImage: 我将使白框在顶部具有可拉伸性,并使用colorWithPatternImage将背景图案填充为背景:

patternView.backgroundColor = [UIColor colorWithPatternImage:@"myBackgroundPattern.png"];

and the black stroke could be done with: 黑色笔触可以通过以下方式完成:

#include <QuartzCore/QuartzCore.h>
...
patternView.layer.borderWidth = 2.f;
patternView.layer.borderColor = [[UIColor blackColor] CGColor];

What you are trying to achieve can be done using the resizableImageWithCapInsets method of a UIImage. 您可以尝试使用UIImage的resizableImageWithCapInsets方法来实现。 This way, you can set cap widths on each side of the image and specify which part of the image needs to be repeated (not stretched). 这样,您可以在图像的每一侧设置帽宽,并指定需要重复(不拉伸)图像的哪一部分。

An UIEdgeInset is a struct consisting of four components as follows (CGFloat topCapWith, CGFloat leftCapWith, CGFloat bottomCapWith, CGFloat rightCapWith) UIEdgeInset是由以下四个组件组成的结构(CGFloat topCapWith,CGFloat leftCapWith,CGFloat bottomCapWith,CGFloat rightCapWith)

The code below would do the trick 下面的代码可以解决问题

UIImage * backgroundImage = [[UIImage imageNamed:@"Stretch.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(80, 4, 4, 10)];
    backgroundImageView.image = backgroundImage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM