Search code examples
swiftswiftuialignmentframeios14

Alignment not working in ios14, xcode12 swiftUI


My alignment is not working in xcode12 ios14, however, I had same code in xcode11.5 everything was fine . whenever I have added .frame viewModifier to the content alignment is not in the center, is it a bug or has API changed? Here is code : Please check it out in xcode12 , ios14 only.

struct ContentView: View {
    
    var body : some View {
       
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50)
            
        }
        
    }
    
    
   
}

This is also not working:

struct ContentView: View {
    
    var body : some View {
        ZStack(alignment: .center) {
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50)
            
        }
        
    }
    }
    
   
}

This not working :

struct ContentView: View {
    
    var body : some View {
        ZStack(alignment: .center) {
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50, alignment: .center)
            
        }
        
    }
    }
    
   
}

Whenever I remove GeometryReader everything is working fine but I want GeometryReader


Solution

  • The following gives you centred capsule in both cases

    ZStack {
        Capsule()
            .fill(Color.red)
            .frame(height: 50)
    }.frame(maxWidth: .infinity, maxHeight: .infinity)