简体   繁体   English

如何使 Swift 中的图像与旁边的文本高度相同?

[英]How do I make an Image in Swift the same height as the text next to it?

I'm brand new to Swift and am making an iOS Minesweeper app.我是 Swift 的新手,正在制作 iOS 扫雷应用程序。 At the top of the screen I want to have an image of a bomb next to a Text() object displaying the number of bombs remaining.在屏幕顶部,我想在 Text() object 旁边显示炸弹的图像,显示剩余的炸弹数量。

I'd like to have the image and/or HStack resize to whatever the height of the adjacent text is without having to hard-code it's dimensions.我想让图像和/或 HStack 调整到相邻文本的高度,而不必对其尺寸进行硬编码。

Can/how this be achieved?可以/如何实现?

The code so far looks like this:到目前为止的代码如下所示:

HStack(alignment: .center, spacing: 10) {
    Image("top_bomb")
        .resizable()
        .aspectRatio(contentMode: .fit)
    Text(String(game.bombsRemaining))
        .font(.system(size: 30, weight: .bold, design: .monospaced))
}
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1, alignment: .trailing)

And it looks like:它看起来像: 扫雷应用

@State var labelHeight = CGFloat.zero

HStack(alignment: .center, spacing: 10) {
    Image("top_bomb")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(maxHeight: labelHeight)
    Text(String(game.bombsRemaining))
        .font(.system(size: 30, weight: .bold, design: .monospaced))
        .overlay(
            GeometryReader(content: { geometry in
                Color.clear
                    .onAppear(perform: {
                        self.labelHeight = geometry.frame(in: .local).size.height
                    })
            })
        )
}
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.2, alignment: .trailing)

You just need to fix it by size, like你只需要按大小修复它,比如

HStack(alignment: .center, spacing: 10) {
    Image("top_bomb")
        .resizable()
        .aspectRatio(contentMode: .fit)
    Text(String(game.bombsRemaining))
        .font(.system(size: 30, weight: .bold, design: .monospaced))
}
.fixedSize()                                        // << here !!
.frame(maxWidth: .infinity, alignment: .trailing)   // << !!

Note: also pay attention that you don't need to hardcode frame to screen size注意:还请注意,您不需要将帧硬编码为屏幕大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Swift将文本字段的文本作为下一个导航控制器的标题? - How do I make a text field's text the title of the next navigation controller using Swift? 如何在iPhone 6/6 plus上使UITableViewCells高度相同? - How do I make UITableViewCells same height on iPhone 6/6 plus? 如何在 Swift 中设置图像的宽度和高度 - How do set a width and height of an Image in Swift Swift如何使导航中间部分的图像和文本彼此相邻 - Swift how to make for navigation middle section image and text next to each other 如何在 iOS Swift 中向图像添加文本? - How do I add text to an image in iOS Swift? 如何使光标的高度与 UITextField 中的文本高度相同? - How to make the height of the cursor same with the height of text in UITextField? 如何在Swift Xcode中制作图像重量和高度填充屏幕? - How to make image weight and height fill screen in Swift Xcode? 如何在Swift中根据图像的高度设置UITableViewCell Size - How to make UITableViewCell Size according to the height of the image in Swift 如何根据标签高度快速制作可拉伸图像 - How to make a strechable image according to label height in swift 如何快速更新y的文本输入位置,具体取决于键盘高度 - How do I update text input position of the y depending on keyboard height in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM