将视图控制器主视图(self.view)子视图移动到scrollview

时间:2015-08-13 16:22:31

标签: ios swift user-interface

  1. 我有视图控制器的子视图&应用约束。
  2. 问题:我要做的是以编程方式添加scrollview 只有iPhone 4s设备在scrollview中包含self.view的所有子视图。
  3. scrollview应包含self.view的所有子视图(我使用self.view.subviews添加)。

    1. 我试过的代码
    2. 如果DeviceType.IS_IPHONE_4_OR_LESS {

          //Add Scrollview to self.view
          let scrollview : UIScrollView = UIScrollView(frame: CGRectMake(0,0,320,480))
          scrollview.showsVerticalScrollIndicator = true;
          scrollview.scrollEnabled = true;
          scrollview.userInteractionEnabled = true;
      
         // scrollview.addSubview(self.view) /
      
          let total : NSArray = (self.view.subviews as NSArray).copy() as! NSArray
      
          //Add elements to scrollview but subviewes don't get scroll
          for subview in total
          {
              scrollview.addSubview(subview as! UIView)
          }
          /// Results in blank white screen. Reason: May be scrollview & self.view pointing to same objects
          for subview in self.view.subviews {
      
              subview.removeFromSuperview()
          }
      
         self.view.addSubview(scrollview)
      
          scrollview.contentSize = CGSizeMake(self.view.bounds.size.width,  self.view.bounds.size.height+200)
      
          }
      
    3. 我知道Size class&给问题可以解决。

    4. 我也知道在故事板中添加scrollview&隐藏所有设备,除了iPhone 4s可能有效。

0 个答案:

没有答案