如何制作圆形 UIView [英] How to make a circular UIView

查看:22
本文介绍了如何制作圆形 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个圆形的 UIView 或 UIImageView.或者我可以使用滑块更改大小和使用选择器视图更改颜色的圆圈.

I want to make a UIView or UIImageView that is a circle. Or a circle that i can change the size of using a slider, and the color of with a pickerview.

推荐答案

正如一些评论中提到的,@IBDesignable 现在使这变得更加容易,因此您可以使用 Interface Builder 来配置您的圆形 UIImageView.

As mentioned in some comments, @IBDesignable makes this much easier now, so you can use Interface Builder to configure your rounded UIImageView.

首先创建一个名为 RoundedImageView.swift 的类并将这段代码粘贴到其中:

First create a class named RoundedImageView.swift and paste this code to it:

import UIKit

@IBDesignable public class RoundedImageView: UIImageView {

    override public func layoutSubviews() {
        super.layoutSubviews()

        //hard-coded this since it's always round
        layer.cornerRadius = 0.5 * bounds.size.width
    }
}

在InterfaceBuilder中选择UIImageView,将类从UIImageView改为自定义的RoundedImageView:

Select the UIImageView in InterfaceBuilder and change the class from UIImageView to the custom RoundedImageView:

Clip to Bounds 设置为 true(否则图片将超出圆圈):

Set Clip to Bounds to true (or the pic will extend beyond the circle):

它现在应该在 InterfaceBuilder 中自己圆了,这非常漂亮.确保将宽度和高度设置为相同的值,否则它会像齐柏林飞艇一样!

It should now round itself right there in InterfaceBuilder, which is pretty nifty. Be sure to set the width and height to the same values or it'll be shaped like a zeppelin!

这篇关于如何制作圆形 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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