iPhone自动布局图像排列问题

时间:2014-05-16 13:26:35

标签: ios iphone ipad autolayout

我想在iPhone和iPhone上安排3张图片iPad使用自动布局。

3图像应重新保留纵横比,并且所有3个图像的宽度应相同。 所有3张图片的左右两侧空间相同。

请参阅此链接上的示例图(图中显示横向和纵向模式):

肖像:http://i.imgur.com/9KVXATE.png

风景:http://i.imgur.com/tDjj9K6.png

可以以编程方式获取屏幕/视图的宽度和高度,但我想使用自动布局

编程方式:

//Inside this method
//- (void) didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation

//Main_View is the view in which the 3 images are kept
//ImageView1, ImageView2, ImageView3 are 3 image views

ImageView1.frame = CGRectMake(Main_View.frame.origin.x + 3, Main_View.frame.origin.y + 30, ((Main_View.frame.size.width / 3)-4), ((Main_View.frame.size.width / 3) - 4) * (82.0/75.0));


ImageView2.frame = CGRectMake(ImageView1.frame.size.width + 6, Main_View.frame.origin.y + 30, ((Main_View.frame.size.width / 3)-4), ((Main_View.frame.size.width / 3) - 4) * (82.0/75.0));


ImageView3.frame = CGRectMake(ImageView2.frame.size.width + ImageView1.frame.size.width + 9, Main_View.frame.origin.y + 30, ((Main_View.frame.size.width / 3)-4), ((Main_View.frame.size.width / 3) - 4) * (82.0/75.0));

Main_View更改其大小(宽度,高度)。然后(ImageView1宽度)是(Main_View width / 3),它也保留宽高比。

以编程方式完成它

2 个答案:

答案 0 :(得分:1)

使用interfacebuilder中的autolayout很容易做到这一点。但是,在界面构建器中使用autolayout时,您可能不得不牺牲一些东西。您可能希望在故事板中安排项目。执行此操作的步骤如下:

  1. 向界面构建器添加三个视图,如此,
  2. enter image description here

    1. 选择所有三张图片,并在界面构建器中添加新约束,使用此按钮| --- |,并选择等宽和相等高度。
    2. enter image description here

      1. 然后,再次选择所有三个视图,然后从菜单中选择编辑器 - >对齐 - >顶边
      2. enter image description here

        1. 选择最左侧的视图并向左侧添加一些偏移
        2. enter image description here

          1. 选择最右侧的视图并向右侧添加一些偏移
          2. enter image description here

            1. 选择其中一个视图,并在顶部和底部给出一些偏移。
            2. enter image description here

              1. 选择第二个视图控件+将其拖动到第一个并选择水平分色并给出一些常量值,并对第二个和第三个视图执行相同操作。
              2. enter image description here

                1. 现在,约束已经完成。可能会发生这种情况,当您同时选择多个视图并添加一些约束时,可能不会将其添加到其中一个视图中。因此,如果我们添加了所有这些约束,那么可能必须解决这个看起来错误并查看视图的约束。

                2. 您可以保留将视图与上边距或下边距分开的约束,并为其创建插座,然后根据需要进行更改,或者您可以在故事板中设置一些参数。

                3. 以下是ipad模拟器的水平方向截图。

                  Verical Orientation

                  这是垂直方向,

                  enter image description here

                  我希望我已经回答了你的问题。如果你对此有所了解,请告诉我。

答案 1 :(得分:0)

  • (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    // i assume here that you have three imageviews img1, img2, img3
    

    float viewWidth = [UIScreen mainScreen] .bounds.size.width; float viewHeight = [UIScreen mainScreen] .bounds.size.height;

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait){     //你的肖像模式代码在这里 } 其他{     NSLog(@“界限是(%f,%f)”,viewHeight,viewWidth);

    img1.width = viewHeight/3; //i used "viewHeight" because of landscape mode
    img2.width = viewHeight/3;
    img3.width = viewHeight/3;
        // and then set centre of imageview according to your requirement
    

    } }