2016-02-22 155 views
0

我正在尝试使图像视图为配置文件图片圈起来。 这是工作正常之前,我已经把UiScreen宽度的限制。圆形图像视图

所以这里是代码

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var imageView: UIImageView! 

@IBOutlet weak var proPicH: NSLayoutConstraint! //Profile Picture Height 
@IBOutlet weak var proPicW: NSLayoutConstraint! // Profile Picture Width 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    circleImage() 
} 

func circleImage() { 
    let screenSize = UIScreen.mainScreen().bounds 
    let screenWidth = screenSize.width 
    let width = screenWidth/2 
    print("ScreenWidth: \(screenWidth)") 
    proPicW.constant = width 
    proPicH.constant = width 

    print("H:\(proPicH.constant)") 
    print("W:\(proPicW.constant)") //Here the height and width comes to my expectations 

    imageView.layer.cornerRadius = imageView.bounds.width/2 
    imageView.clipsToBounds = true 

    print("Height: \(imageView.bounds.height)") // Here the height and width becomes more 
} 
} 

Here is how image comes, I want it to be round

请帮我这个做形象轮

+0

在哪里添加这2个常量?在'视图'或'UIImageView'中? – TechBee

+0

使用Imageview的宽度并将其设置为圆角半径以使您的imageview圆圈。 –

+0

这两个约束是UIImageView,即高度和宽度.. –

回答

1

在你设置的时间点图像视图圆角半径的范围避风港还没有更新以符合约束条件。改变这一行

imageView.layer.cornerRadius = imageView.bounds.width/2 

imageView.layer.cornerRadius = width/2 

这样用于设置限制相同的值也用于拐角半径。

请注意,如果您更新其他一些代码中的约束条件,您也可以更新角点半径以进行匹配。

0

我建议你把

circleImage() 

在viewDidAppears方法

0

使用imageView.layer.cornerRadius = imageView.frame.size.width/2 imageView.layer.maskToBounds =是;