Swift - UIView缩放

时间:2014-11-29 02:03:12

标签: ios swift uiview

问题:

适当地调整子视图(或子视图集)的大小以使它们适合(缩放)到适当的边界 - 或者在这种情况下确保蓝色圆圈适合黄色方块。下面的图片和游乐场代码!

enter image description here

import UIKit


let topLevelFrame = CGRectMake(0, 0, 400, 400)

let topView = UIView(frame: topLevelFrame)
topView.backgroundColor = UIColor.grayColor()
topView


let windowFrame = CGRectMake(20, 20, 300, 300)
let windowView = UIView(frame: windowFrame)
windowView.backgroundColor = UIColor.yellowColor()
topView.addSubview(windowView)

topView

let contentFrame = CGRectMake(-200, -200, 400, 400)
let contentView = UIView(frame: contentFrame)
//contentView.alpha = 5
contentView.layer.cornerRadius=200
contentView.backgroundColor = UIColor.blueColor()

contentView


windowView.clipsToBounds = true
windowView.addSubview(contentView)

windowView.bounds
windowView.frame


// How do I fit the blue circle to scale appropriately inside the yellow square?

topView

1 个答案:

答案 0 :(得分:1)

尝试将代码更改为以下内容:

let contentFrame = CGRectMake(windowFrame.origin.x, windowFrame.origin.y, windowFrame.size.width, windowFrame.size.height)
let contentView = UIView(frame: contentFrame)

contentView.layer.cornerRadius = windowFrame.size.height / 2.0
contentView.center = CGPoint(x:windowFrame.size.width / 2.0, y:windowFrame.size.height / 2.0)

contentView.backgroundColor = UIColor.blueColor()

contentView