在 iOS11 中尝试将圆形图像添加为 NavigationBar 中的 navigationItem 并且它没有剪裁其边缘
In iOS11 trying to add a round image as navigationItem in a NavigationBar and it is not clipping its edges
使用 iOS13.3, XCode11.3,
我尝试在我的 largeTitle NavigationBar 上放置一个圆形图像作为 rightBarButtonItem。
按钮已绘制,但未被剪裁,并且残留有白色的难看角。
这是我的代码(见下文):
如您所见,我向其中添加了 clipsToBounds = true
- 但我没有看到边缘被剪裁。为什么???
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
您正在 navigation bar
中添加按钮并将 clipToBounds
设置为 profileImage
给按钮而不是图像的角半径将解决您的问题。
目前您的按钮尺寸为 36x36
使用 iOS13.3, XCode11.3,
我尝试在我的 largeTitle NavigationBar 上放置一个圆形图像作为 rightBarButtonItem。
按钮已绘制,但未被剪裁,并且残留有白色的难看角。
这是我的代码(见下文):
如您所见,我向其中添加了 clipsToBounds = true
- 但我没有看到边缘被剪裁。为什么???
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
您正在 navigation bar
中添加按钮并将 clipToBounds
设置为 profileImage
给按钮而不是图像的角半径将解决您的问题。
目前您的按钮尺寸为 36x36