gpt4 book ai didi

ios - Swift - UIView 缩放

转载 作者:行者123 更新时间:2023-11-28 07:13:55 25 4
gpt4 key购买 nike

问题:

什么是适合调整 subview (或 subview 集)的大小以使其适合(缩放)到适当的边界 - 或者在这种情况下确保蓝色圆圈适合黄色方 block 。下面的图片和 Playground 代码!

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

最佳答案

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

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

关于ios - Swift - UIView 缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com