小编imx*_*eyi的帖子

为什么自定义导航栏按钮图像被拉伸?

我试图添加带有自定义图像的导航栏按钮。但是,无论我使用哪种方法,图像总是看起来被拉伸。

方法1:

    let barbuttonitem = UIBarButtonItem(image: UIImage(named: "apps_small"), style: .plain, target: self, action: nil)
    navigationItem.leftBarButtonItem = barbuttonitem
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

方法1

方法2:

    let button = UIButton(type: .custom)
    button.setImage(UIImage(named: "apps_small"), for: .normal)
    button.addTarget(self, action: #selector(TeachingRootViewController.appsTouched), for: .touchUpInside)
    button.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    button.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

方法2

方法3:

    let button = UIButton(type: .custom)
    button.setImage(UIImage(named: "apps_small"), for: .normal)
    button.setTitle("Button", for: .normal)
    button.frame = CGRect(x: 0, y: 0, width: 10, height: …
Run Code Online (Sandbox Code Playgroud)

image button uinavigationbar uinavigationitem ios

5
推荐指数
1
解决办法
892
查看次数

SwiftUI 不释放列表中已删除单元格的内存

如果您创建以下视图的列表作为行:

struct ImageCellView: View {
    let image: UIImage

    init(image: UIImage) {
        self.image = image
    }

    var body: some View {
        return Image(uiImage: image).frame(height: 128).clipped()
    }
}
Run Code Online (Sandbox Code Playgroud)

删除单元格后,UIImage 的内存不会释放,直到添加新单元格。查看内存图,我们可以发现 AGGraphStorage 持有对此的引用:

即使添加新单元格可以以某种方式释放内存,UIImage 偶尔也会泄漏:

这是 SwiftUI 中的错误吗?有什么办法可以摆脱这种行为吗?

ios swift swiftui

5
推荐指数
1
解决办法
1021
查看次数

标签 统计

ios ×2

button ×1

image ×1

swift ×1

swiftui ×1

uinavigationbar ×1

uinavigationitem ×1