【问题标题】:How to shift subviews downwards if a subview goes out of screen bounds in ios如果子视图在ios中超出屏幕范围,如何向下移动子视图
【发布时间】:2017-06-21 11:11:42
【问题描述】:

我正在开发一个在 Imageview 上显示多个图像的视图,并且图像的数量是动态的。 我通过 URL 获取这些图像,并且 URL 的字符串存储在一个数组中。

我采用了一个 for 循环来为要在其上显示的每个图像创建一个新的图像视图。 imageview 的 frame 是根据图像的大小来设置的。但问题是,当图像数量超过 5 个时,第 6 个图像就会出现在屏幕之外。但是如果数组中有超过 5 个图像,我希望它向下移动。

下面是我使用 for-loop 在 imageview 上显示图像的代码

int x=0;
for (int i=0;i<[items count]; i++)
{
    NSLog(@"image name %@",[items objectAtIndex:i]);
    NSLog(@"image url  %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);

    NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];        

    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
    imgV.frame=CGRectMake(x, 0, 50, 50);
    [cell.contentView addSubview:imgV];
    x=x+imgV.frame.size.width+10;
}

【问题讨论】:

  • 为什么不使用表格视图或滚动视图??

标签: ios objective-c image for-loop uiimageview


【解决方案1】:

像这张图片一样移动

int x=0;
int y =0;
 for (int i=0;i<[items count]; i++)
{
  NSLog(@"image name %@",[items objectAtIndex:i]);
  NSLog(@"image url  %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);

   NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];        

   UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
   imgV.frame=CGRectMake(x, y, 50, 50);
   [cell.contentView addSubview:imgV];
   if(x>cell.frame.size.width)
     {
         x =0;
         y= y+imgV.frame.size.height+10;
     }
     else
     {
      x=x+imgV.frame.size.width+10;

     }
 }

【讨论】:

    【解决方案2】:

    您可以在滚动视图或表格视图中显示图像,这样当有许多图像并且您无法将所有这些图像都放在屏幕上时。然后它将具有滚动功能。

    【讨论】:

      【解决方案3】:

      如果您正在使用情节提要,请将 scrollview 添加到您的 viewController 的视图中,然后使用此示例代码应该会对您有所帮助。

      #import "ViewController.h"
      
      @interface ViewController ()
      @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
          [self setAutomaticallyAdjustsScrollViewInsets:YES];
      
          [self addView:15];
      }
      
      
      - (void)didReceiveMemoryWarning {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      -(void) addView:(NSInteger)numberOfViews
      {
          double x =10 , y=10;
      
          for (NSInteger i=1; i <= numberOfViews; i++) {
              UIView * view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];
              y = y+70;
              [view setBackgroundColor:[UIColor purpleColor]];
              [_scrollView addSubview:view];
          }
      }
      @end
      

      注意:在界面构建器中,单击您的滚动视图,然后单击属性检查器并检查(勾选)滚动视图部分下的垂直弹跳选项

      【讨论】:

        猜你喜欢
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多