斯威夫特:两个UIViews的不同大小

时间:2021-05-07 11:59:46

I have a problem respective to UIView and UIView subclasses. In my storyboard I add a UIView (width:368 height:552, center horizontally and vertically). I have connected this UIView to my ViewController:

我有一个与UIView和UIView子类相关的问题。在我的故事板中,我添加了一个UIView(宽度:368高度:552,水平和垂直居中)。我已将此UIView连接到我的ViewController:

 @IBOutlet weak var mapViewOutlet: UIView!

The I want to create a object of my own subclass of UIView:

我想创建一个我自己的UIView子类的对象:

 var bounds:CGRect = CGRectMake(0, 0, mapViewOutlet.frame.size.width, mapViewOutlet.frame.size.height)
 self.roomView     = RoomView(frame: bounds, image: testMap)

The first parameter of the init method is bounds. There I give the size of mapViewOutlet to the subclass. The image parameter is the green image (you can see this in the attached image). But now the size of the green image does not fit the size of the gray mapViewOutlet.

init方法的第一个参数是bounds。在那里,我将mapViewOutlet的大小赋予子类。图像参数是绿色图像(您可以在附加图像中看到这一点)。但现在绿色图像的大小不符合灰色mapViewOutlet的大小。

The green UiView is generated as follows:

绿色UiView生成如下:

    let testFrame : CGRect = CGRectMake(0,0,frame.size.width ,frame.size.height)
    var testView : UIView = UIView(frame: testFrame)
    testView.backgroundColor = UIColor(red: 0.5, green: 4, blue: 0.5, alpha: 1.0)
    testView.alpha=0.5
    self.addSubview(testView)

Does anybody know how they do not fit?

有人知道他们不适合吗?

Thanks.

谢谢。

斯威夫特:两个UIViews的不同大小

1 个解决方案

#1


0  

Now I know the problem. The sizes are wrong only in the viewDidLoad(). The following code gives the correct values of the width and height.

现在我知道了这个问题。仅在viewDidLoad()中大小错误。以下代码给出了正确的宽度和高度值。

 override func viewDidLayoutSubviews() {
  println(self.mapViewOutlet.frame.size)
}

#1


0  

Now I know the problem. The sizes are wrong only in the viewDidLoad(). The following code gives the correct values of the width and height.

现在我知道了这个问题。仅在viewDidLoad()中大小错误。以下代码给出了正确的宽度和高度值。

 override func viewDidLayoutSubviews() {
  println(self.mapViewOutlet.frame.size)
}