【问题标题】:iPhone: Change UIImageView frameiPhone:更改 UIImageView 框架
【发布时间】:2012-03-09 09:08:14
【问题描述】:

我有一个自定义 UIView。所以 myView 类扩展了 UIView。在 initWithFrame:(CGRect)frame 方法中,我将 UIView 的 UIImageView 设置为背景图像。

- (id)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
 if (self) {
        self.bgImage = [[[UIImageView alloc] initWithFrame:
            CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
        int stretchableCap = 20;
        UIImage *bg = [UIImage imageNamed:@"myImage.png"];
        UIImage *stretchIcon = [bg 
            stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
    [self.bgImage setImage:stretchIcon];
 }
return self;
}

所以当我创建我的视图时,一切都很好。

 MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

但如果我想将视图大小更改为更大或更小:

 customView.frame = CGRectMake(10, 10, 50, 50)];

那么我的背景图片有问题。因为它的大小没有改变。该怎么办 ?何与视图框架一起改变背景图像大小?

【问题讨论】:

    标签: iphone uiview uiimageview


    【解决方案1】:

    尝试设置UIImageView的autoresizing mask,它会根据super view的大小变化来调整大小。

    【讨论】:

    • customView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    • 我可以把 self.autoresizesSubviews = YES;到 initWithFrame 方法?
    【解决方案2】:

    我的建议是当你想改变框架时使用更新功能来更新你的自定义视图:

    - (id)initWithFrame:(CGRect)frame
    {
         self = [super initWithFrame:frame];
         if (self) {
             [self UpdateViewWithFrame:frame];
         }
         return self;
    }
    
    - (void)UpdateViewWithFrame:(CGRect)frame
    {
        self.frame = frame;//set frame
        if(!self.bgImage)
        {
             //initialize your imageview
             self.bgImage = [[[UIImageView alloc] initWithFrame:
                CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
        }
        int stretchableCap = 20;
        UIImage *bg = [UIImage imageNamed:@"myImage.png"];
        UIImage *stretchIcon = [bg 
             stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
        [self.bgImage setImage:stretchIcon];
    }
    

    当你想改变框架时,只需调用这个函数。试一试。

    【讨论】:

    • 不是 initWithFrame 只是 UpdateViewWithFrame [customView UpdateViewWithFrame:CGRectMake(10, 10, 50, 50)]
    • 我认为自动调整大小的想法更好
    【解决方案3】:

    正确设置 imageview 所需的 Autoresizingmask 属性属性,因为它必须与视图框架一起调整。

    【讨论】:

      【解决方案4】:

      所以建议是正确的。我应该添加自动调整大小的蒙版。现在我的 initWithFrame 看起来像这样:

      - (id)initWithFrame:(CGRect)frame
      {
      self = [super initWithFrame:frame];
      if (self) {
            self.bgImage = [[[UIImageView alloc] initWithFrame:
              CGRectMake(0, 0, frame.size.width , frame.size.height)] autorelease];
          int stretchableCap = 20;
          UIImage *bg = [UIImage imageNamed:@"myImage.png"];
          UIImage *stretchIcon = [bg 
              stretchableImageWithLeftCapWidth:stretchableCap topCapHeight:0];
      [self.bgImage setImage:stretchIcon];
      self.bgImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
      self.autoresizesSubviews = YES;
      }
      return self;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-22
        • 2011-10-09
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        • 2011-06-02
        相关资源
        最近更新 更多