Home > OS >  Changing the spacing of the underline of a UIButton
Changing the spacing of the underline of a UIButton

Time:08-20

How can I change the spacing between a UIButton and its underline? And also try to make the underline bolder. Any help is appreciated.

My UIButton: Button with bar underneath

In this example you can adjust the spacing by adding a (positive or negative) offset. You can add a corner radius to the bar and ich you wanted to have it rounded on the edges, you also can use Capsule() instead of Rectangle()

UPDATE:

UIKit

Regarding UIKit, I think a possible solution could be to create a view with 2 subviews (1. text, 2. bar underneath the text) and then you put a clear button with the same size over it on the same position.

I'll try to add an UIKit example in the afternoon.

Best, Sebastian

CodePudding user response:

In SwiftUI, there is a View called Rectangle that is perfectly matched for this. You can add it below any view by embedding them into a simple VStack. Here is the code:

import SwiftUI

struct ContentView: View {
    var body: some View {
       
        Button(action: {
        }) {
            VStack(spacing: 15){
                Text("帖子")
                    .font(.system(size: 30))
                Rectangle()
            }
            .frame(width: 70, height: 60, alignment: .center)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Here is the result:

enter image description here

  • Related