【问题标题】:Swift - UIView scalingSwift - UIView 缩放
【发布时间】:2014-11-29 02:03:11
【问题描述】:

问题:

什么是合适的大小子视图(或一组子视图),以便它们适合(缩放)到适当的边界 - 或者在这种情况下确保蓝色圆圈适合黄色方块。图片和操场代码如下!

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

【问题讨论】:

    标签: ios swift uiview


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多