首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS11中,尝试将圆形图像添加为NavigationBar中的navigationItem,但无法裁剪其边缘

在iOS11中,尝试将圆形图像添加为NavigationBar中的navigationItem,但无法裁剪其边缘
EN

Stack Overflow用户
提问于 2020-01-15 22:47:28
回答 1查看 38关注 0票数 0

使用iOS13.3,XCode11.3,

我尝试在我的largeTitle NavigationBar上放置一个圆形图像作为rightBarButtonItem。

按钮被绘制了,但是它没有被裁剪,还有一些丑陋的白色边角。

下面是我的代码(如下所示):

正如您所看到的,我添加了clipsToBounds = true到它-但我没有看到边缘被裁剪。为什么?

代码语言:javascript
复制
if let image = image {
    self.profileImage.image = image
} else {
    self.profileImage.image = #imageLiteral(resourceName: "profile-placeholder-small")
}

let button = UIButton(type: .custom)
button.setImage(self.profileImage.image, for: .normal)
button.addTarget(self, action:#selector(self.callMethod), for: .touchDragInside)
button.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton

button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 36).isActive = true
button.heightAnchor.constraint(equalToConstant: 36).isActive = true

self.profileImage.contentMode = .scaleAspectFit
self.profileImage.layer.cornerRadius = self.profileImage.frame.size.width / 2
self.profileImage.layer.masksToBounds = false
self.profileImage.clipsToBounds = true
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 00:05:24

您在navigation bar中添加按钮,并将clipToBounds设置为profileImage,将角半径提供给按钮而不是图像将解决您的问题。当前您的按钮大小为36x36

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59753799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档