【问题标题】:Position elements according to screen size in Objective C, ios6, Xcode在 Objective C、ios6、Xcode 中根据屏幕大小定位元素
【发布时间】:2013-01-28 08:54:34
【问题描述】:

我有一个从左到右水平滚动的 UIScrollView。

我希望能够根据 iPhone 的屏幕尺寸在 Y 轴上垂直定位我的 UIScrollView。例如 iPhone 4 和 iPhone 5。

CGFloat startX = (70.0f * ((float)[_attachments count] - 1.0f) + padding);
CGFloat startY = 295;
CGFloat width = 64;
CGFloat height = 64; 

现在我的 Y 位置从 295 开始,这适用于 iPhone 4,但不适用于 iPhone 5。

如何更改 startY 以适应不同屏幕尺寸的相同位置?

【问题讨论】:

    标签: iphone objective-c xcode ios6 uiscrollview


    【解决方案1】:
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if (screenBounds.size.height == 568)
    {
         //iphone5
    }
    else
    {
         //iphone4
    }
    

    您可以相应地设置您的 y 值

    【讨论】:

      【解决方案2】:

      请在您的视图中使用layoutSubviews ...

      - (void)layoutSubviews {
        CGRect frame = self.bounds;
        frame.origin.y += frame.size.height - _scrollView.frame.size.height;
        if ( ! CGRectEqualToRect( _scrollView.frame, frame ) {
          _scrollView.frame = frame;
        }
      }
      

      ...我假设您的水平滚动视图位于_scrollView ivar 中,并且_scrollView.frame.size.height 包含您的_scrollView 的正确高度。如果不是,请将_scrollView.frame.size.height 替换为包含UIScrollView 高度的常量。

      为什么是CGRectEqualToRect 的条件?因为layoutSubviews 可以经常被调用,并且当您为其设置新框架(即使它等于)时,UIScrollView 可以停止滚动。

      【讨论】:

        【解决方案3】:

        你可以这样做-

        先获取屏幕的宽高

        CGRect screenBounds = [UIScreen mainScreen].bounds ;
        CGFloat width = CGRectGetWidth(screenBounds);
        CGFloat height = CGRectGetHeight(screenBounds)  ;
        

        现在创建一个方法来进行一些计算并返回一个 int

        -(NSInteger) getwidth:-(NSInteger *) value
        {
        int temp = (value * 100) / 480;
        return int((height * temp) / 100);
        }
        
        -(NSInteger) getheight:-(NSInteger *) value
        {
         var temp = (value * 100) / 320;
        return int((width * temp) / 100);
        }
        

        现在根据这些功能设置视图的边界

        actind.frame=CGRectMake(getWidth(20),getheight(20),16,16);
        

        这将适用于所有设备,并且视图将根据屏幕的宽度和高度自行排列。

        希望这对您有所帮助。 :)

        【讨论】:

          猜你喜欢
          • 2014-10-08
          • 2020-09-29
          • 1970-01-01
          • 2019-09-04
          • 2013-05-12
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          • 2018-03-15
          相关资源
          最近更新 更多