我想制作一个圆形的UIView或UIImageView.或者我可以改变使用滑块大小的圆圈,以及带有选择器视图的颜色.
在storyboard(xcode 6)中,我想从Facebook获取一个圆形用户图像配置文件.
所以我在故事板中使用自动布局制作了这个界面:

然后,使用Facebook iOS sdk我获取用户配置文件(使用swift):
var facebookProfileUrl = "http://graph.facebook.com/\(userId!)/picture?type=normal";
Run Code Online (Sandbox Code Playgroud)
在故事板中,我已将图像设置为"缩放至适合"模式.要使图像视图为圆形,请使用以下代码:
self.facebookProfileImage.layer.cornerRadius = self.facebookProfileImage.frame.size.width / 2;
self.facebookProfileImage.clipsToBounds = true;
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,无论如何图像看起来不是圆形的:

我想问题是自动布局,但我不确定.如何使图像完美圆形?
我正在尝试创建自定义形状NSButton.特别是我试图使用自定义图像制作一个圆形按钮.我找到了一个关于自定义创建的教程,UIButton并尝试将其改编为NSButton.但是有一个很大的问题.clipsToBounds似乎只是iOS(
这是我的代码:
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var mainButton: NSButton!
var size = 32
override func viewDidLoad() {
super.viewDidLoad()
configureButton()
// Do any additional setup after loading the view.
}
func configureButton()
{
mainButton.wantsLayer = true
mainButton.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay
mainButton.layer?.cornerRadius = 0.5 * mainButton.bounds.size.width
mainButton.layer?.borderColor = NSColor(red:0.0/255.0, green:122.0/255.0, blue:255.0/255.0, alpha:1).CGColor as CGColorRef
mainButton.layer?.borderWidth = 2.0
}
override var representedObject: AnyObject? {
didSet {
// Update the view, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将圆角应用于UIImageView.尽管角落是圆形的,但仍然有4个边缘,无论半径大小,它都不会消失.删除边框也没有帮助.
这可能与自动布局约束有关吗?我究竟做错了什么?
这是我正在申请的代码:
self.imageViewProfilePicture.layer.cornerRadius = self.imageViewProfilePicture.frame.size.width / 2.0
self.imageViewProfilePicture.layer.borderWidth = 2.0
self.imageViewProfilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.imageViewProfilePicture.layer.masksToBounds = true
self.imageViewProfilePicture.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
ios ×2
uiimageview ×2
autolayout ×1
geometry ×1
iphone ×1
macos ×1
nsbutton ×1
objective-c ×1
quartz-core ×1
storyboard ×1
swift ×1
uiview ×1
xcode ×1