根据百分比用颜色填充 UIView [英] Fill UIView with color based on percentage

查看:17
本文介绍了根据百分比用颜色填充 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIViewController 的 UIView 中有一个 UIView,高度 = 149.0,宽度 = 123.5.

I have a UIView inside the UIView of UIViewController with Height = 149.0 and Width = 123.5.

我正在用颜色填充这个 UIView,并且我正在根据来自其余 url 的百分比响应设置要填充的颜色的高度.

I am filling this UIView with color, and I am setting the height of the color to be filled based on a response in percentage from a rest url.

如何使用上述尺寸为 100% 和不同的值填充整个视图.

How do I fill the entire View with the above mentioned dimensions for 100% and different values.

任何帮助将不胜感激.谢谢

Any help will be appreciated. Thank you

推荐答案

如果你想在你的 uiview 中填充多种颜色,那么你需要子类化 UIView 并且应该覆盖它是 drawRect 方法,类似于,

If you want to fill multiple color in your uiview then you need to subclass UIView and should override it's drawRect method something like,

 override func drawRect(rect: CGRect) {


    var percentage = CGFloat()  //can declare as instance variable or globally which you get from web service and set it's value!!



    percentage = 0.5

    let upperRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height*percentage)

    let lowerRect = CGRectMake(rect.origin.x, rect.origin.y + (rect.size.height * percentage), rect.size.width, rect.size.height * (1-percentage))

    UIColor.redColor().set()
    UIRectFill(upperRect)
    UIColor.greenColor().set()
    UIRectFill(lowerRect)



}

这篇关于根据百分比用颜色填充 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆