【问题标题】:Adding a UIImageView and handling the size/fit添加 UIImageView 并处理大小/适合
【发布时间】:2013-03-27 21:24:58
【问题描述】:

我创建一个视图如下:

// Create sub view for Logo
logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
[logoView setBackgroundColor:[UIColor blueColor]];
UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
[logoView addSubview:bgLogoView];
//NSLog(@"the button view height is %f",(screen.size.height - 230));
logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

我有一个用于 logoView 的设置框。它的高度为 230 (iphone4

如何使此图像适合视图。目前,它将完整尺寸的图像加载到实际上只有一半大小的块中......图像被制作成两倍大小以提高质量。我正在运行 ios5 的 iphone4 上进行测试。

我确信有一两行遗漏了以某种方式使图像适合周围视图的视图边界。

【问题讨论】:

    标签: ios ios5 uiview uiimageview uiimage


    【解决方案1】:

    您需要重置图像视图的框架。正如你所拥有的,图像视图的框架将是图像的大小。另外,设置图像视图的contentMode。最后,设置图像视图的autoresizingMask

    logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
    [logoView setBackgroundColor:[UIColor blueColor]];
    UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
    bgLogoView.frame = logoView.bounds; // reset the frame
    bgLogoView.contentMode = UIViewContentModeScaleAspectFit; // or other desired mode
    bgLogoView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [logoView addSubview:bgLogoView];
    //NSLog(@"the button view height is %f",(screen.size.height - 230));
    logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    

    【讨论】:

      【解决方案2】:

      类似

      bgLogoView.contentMode = UIViewContentModeScaleAspectFit;
      

      查看 UIView 的 contentMode 属性。可能的值是:

      typedef enum {
         UIViewContentModeScaleToFill,
         UIViewContentModeScaleAspectFit,
         UIViewContentModeScaleAspectFill,
         UIViewContentModeRedraw,
         UIViewContentModeCenter,
         UIViewContentModeTop,
         UIViewContentModeBottom,
         UIViewContentModeLeft,
         UIViewContentModeRight,
         UIViewContentModeTopLeft,
         UIViewContentModeTopRight,
         UIViewContentModeBottomLeft,
         UIViewContentModeBottomRight,
      } UIViewContentMode;
      

      【讨论】:

        【解决方案3】:

        试试这个:

        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230)];
        NSString *bgFilePath = [[NSBundle mainBundle] pathForResource:@"bg-welcomeLogoView"
                                                               ofType:@"png"];
        UIImage *img = [[UIImage alloc] initWithContentsOfFile:bgFilePath];
        [bgImageView setImage:img];
        [logoView addSubview:bgImageView];
        

        您可以在 initWithFrame 中更改 UIImageView 的大小:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-29
          • 2018-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多