我有一个按钮,包括a)图像和b)背景图像 - 都在故事板中构建。由于背景图片具有不同的形状,因此图像从按钮中弹出。我想给实际的图像(a)四面都加一些间距,以便它可以缩回背景图像中。但如果只使用插入来添加间距,它只会给非相对面提供填充;当相反的面设置相同的插入时,图像不会缩小。请告诉我如何在故事板和编程中完成这个功能。
我有一个按钮,包括a)图像和b)背景图像 - 都在故事板中构建。由于背景图片具有不同的形状,因此图像从按钮中弹出。我想给实际的图像(a)四面都加一些间距,以便它可以缩回背景图像中。但如果只使用插入来添加间距,它只会给非相对面提供填充;当相反的面设置相同的插入时,图像不会缩小。请告诉我如何在故事板和编程中完成这个功能。
而对于iOS 15及以上版本
// 1. Create configuration for your button
var configuration = UIButton.Configuration.plain() // there are several options to choose from instead of .plain()
// 2. Here is your content Insets
configuration.contentInsets = NSDirectionalEdgeInsets(top: 24, leading: 24, bottom: 24, trailing: 24)
// 3. This is my Button and you can set to your Button the configuration
myButton.configuration = configuration
// 4. Hehe this step is just for fun, Swift is fun <3
附言:
这个功能不适用于旧版的iOS系统,只适用于15及以上版本。