Swift 4.2仅实现视图
我已经创建了一个programmatically并在其中放置了四个按钮,但它们只是没有任何灰色背景的简单按钮。
我想在我的stackview中实现精确的四个按钮-
ios - 如何获得类似于iOS Maps应用程序模态的插入按钮?-LMLPHP

button.contentEdgeInset //Not working

这就是我试过的-
 let feedbackButton:UIButton = {
    let origImage = UIImage(named: "feedback")
    let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setBackgroundImage(tintedImage, for: UIControl.State.normal)
    button.tintColor = .systemPinkColor
    button.layer.cornerRadius = 7
    button.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
    button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)
//  button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 50)
//  button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 50)
    button.clipsToBounds = true
    button.addTarget(self, action: #selector(feedbackFaculty), for: .touchUpInside)
    return button
}()

输出-
ios - 如何获得类似于iOS Maps应用程序模态的插入按钮?-LMLPHP
按钮的图像随着插入而拉伸。stack viewbutton.imageInsets不工作。
帮帮我。谢谢!

最佳答案

背景图像不跟随内容插入。所以这句话:

button.setBackgroundImage(tintedImage, for: UIControl.State.normal)

需要更改为:
button.setImage(tintedImage, for: .normal)

另外,这里有几点需要注意(根据苹果的UIButton文档):
提供标题字符串或图像;根据内容调整按钮的大小。
这意味着您不能同时设置图像和标题,这可能是您注意到titleInsetimageInset播放不好的原因。
但是,如果您只是为按钮设置适当的约束,并为标题或图像设置插入,则应该能够获得所需的外观。还要确保将堆栈视图配置为对其分布使用相等的间距。
下图中每个按钮的imageInset(20, 20, 20, 20)
ios - 如何获得类似于iOS Maps应用程序模态的插入按钮?-LMLPHP

关于ios - 如何获得类似于iOS Maps应用程序模态的插入按钮?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52670964/

10-12 00:30
查看更多