自动调整XIB文件的大小以作为屏幕大小

时间:2016-07-24 15:59:53

标签: objective-c swift xcode xib

我有一个内置了scrollView的View Controller。在此Scroll视图中,我添加了4个视图,这些视图是XIB文件视图。 XIB文件大小为“推断”,但似乎是iphone5大小调整。加载时,它们不会自动调整大小以适应屏幕尺寸,它们会保持iphone5大小。

如何使XIB文件与加载的屏幕大小相同?

编辑:

我也尝试通过以下代码更改nib文件的大小:

 // 1) Create the three views used in the swipe container view
        let AVc :AViewController =  AViewController(nibName: "AViewController", bundle: nil);
        let BVc :BViewController =  BViewController(nibName: "BViewController", bundle: nil);
        let CVc :CViewController =  CViewController(nibName: "CViewController", bundle: nil); 
        let DVc :DViewController =  DViewController(nibName: "DViewController", bundle: nil);

        AVc.view.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height)
        BVc.view.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height)
        CVc.view.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height)
        DVc.view.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height)

        // 2) Add in each view to the container view hierarchy
        //    Add them in opposite order since the view hieracrhy is a stack

        self.addChildViewController(DVc);
        self.scrollView!.addSubview(DVc.view);
        DVc.didMoveToParentViewController(self);

        self.addChildViewController(CVc);
        self.scrollView!.addSubview(CVc.view);
        CVc.didMoveToParentViewController(self);

        self.addChildViewController(BVc);
        self.scrollView!.addSubview(BVc.view);
        BVc.didMoveToParentViewController(self);

        self.addChildViewController(AVc);
        self.scrollView!.addSubview(AVc.view);
        AVc.didMoveToParentViewController(self);

这仍然没有影响,并且加载了错误的大小。

2 个答案:

答案 0 :(得分:0)

我建议你先使用大小类,然后应用必要的常量,这会有点困难,但是到时候你会学习如何应用大小类和常量。 Here是您可以找到最佳examples的链接。

答案 1 :(得分:0)

如果您没有使用自动布局,则只需通过for循环添加四个视图:

let bounds = UIScreen.mainScreen().bounds
let width = bounds.size.width
let height = bounds.size.height

var yAxis: CGFloat = 0.0

for i in 0...3 {
   let rect: CGRect = CGRectMake(0, yAxis, width, height)

   swift i {
       case 0:
          view1.frame = rect
       case 1:
          view2.frame = rect
       case 2:
          view3.frame = rect
       case 3:
          view4.frame = rect
       default:
          break
   }

   yAxis = yAxis + height
}

其中view1,view2,view3和view4是你的4个xib视图。 另外,将滚动视图的内容大小设置为高度yAxis。

推荐,你应该使用AutoLayout它会减少代码行。