带有圆形底部边缘和项目的 SwiftUI 自定义导航栏

发布于 2025-01-19 09:34:41 字数 384 浏览 4 评论 0原文

我已经在Stackoverflow上搜索了一个解决方案,但没有找到解决方案。

标题中描述的期望目标是:

”在此处输入图像描述

我们所看到的所需颜色也是渐变。

我也想知道,每当我想显示它时,是否也可以在导航栏的右侧显示图像(有时我想显示一个图像,有时我没有)

我知道我必须使用Uikit才能创建类似的东西,但我还没有找到一个解决方案,可以使导航栏的底部边缘变为圆形。

I have searched all over stackoverflow for a solution but I haven't found one.

The desired goal as described in the title is this:

enter image description here

The desired color as we can see is also gradient.

I also wonder if it is possible to also display an image on the right side of the navigation bar whenever I wanted to show it ( sometimes i want to show an image and sometimes i dont)

I know that i have to use UIKIT in order to create something like this but i have not found a solution where i can make the bottom edges of the navigation bar rounded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2025-01-26 09:34:41

无需使用UIKIT,因为您可以使用Path Drawing使用Anative Swiftui来制作这样的视图,并保留注释以隐藏默认的导航视图标题,并查看和使用下面的代码,您也可以根据您的需求自定义

  struct ContentView: View {


   var body: some View {
    ZStack {
        
        CustomShapeNav().fill(
            LinearGradient(gradient: Gradient(colors: [.green, .blue]), 
        startPoint: .top, endPoint: .bottom)
            // use gradient color here
        ).shadow(color: Color(#colorLiteral(red: 0.4745098039, green: 
        0.4745098039, blue: 0.4745098039, alpha: 1)), radius: 
        15).ignoresSafeArea()
        Button {
            print("Button was tapped")
        } label: {
            Image(systemName: "chevron.backward")
                .padding()
                .foregroundColor(.black)
          }.position(x: 40, y: 40)
         }.frame(width: 700, height: 300, alignment: .center)
         }
         }


 //Define the custom navigation using path drawing and make struct confirming to Shape 
struct CustomShapeNav: Shape {
   func path(in rect: CGRect) -> Path {
    Path { path in
        path.move(to: CGPoint(x: 0, y: 0)) // start point from 0 x-axis & 0 y-axis
        path.addLine(to: CGPoint(x: rect.maxX , y: 0)) // draw line max x-axis & 0 y-axis
        path.addLine(to: CGPoint(x: rect.maxX, y: 50)) // draw line max x-axis & 50 y-axis
        path.addLine(to: CGPoint(x: 0, y: 50))  // draw line 0 x-axis & 50 y-axis
        path.move(to: CGPoint(x: 0, y: 50)) // make the start point 0,50 instead of 0,0
        path.addQuadCurve(to: CGPoint(x: 90, y: 90), control: CGPoint(x: 
        0, y: 85)) // add curve to point 90,90 and make the point of curve 0,85
        path.addLine(to: CGPoint(x: rect.maxX - 90, y: 90))  // draw line -90 x-axis & 90 y-axis
        path.addQuadCurve(to: CGPoint(x: rect.maxX, y: 50), control: CGPoint(x: rect.maxX, y: 85))
        // add curve to point max x-axis and 50 and make the point of curve max x-axis and 85
        
        }
        }
        }

视图导航栏将就像该视图

no need to use UIKit in that you can use native SwiftUI using Path drawing to make a view like that and keep a note to hide the default navigation view title and view and use the code below you also can customize the view depending on your needs

  struct ContentView: View {


   var body: some View {
    ZStack {
        
        CustomShapeNav().fill(
            LinearGradient(gradient: Gradient(colors: [.green, .blue]), 
        startPoint: .top, endPoint: .bottom)
            // use gradient color here
        ).shadow(color: Color(#colorLiteral(red: 0.4745098039, green: 
        0.4745098039, blue: 0.4745098039, alpha: 1)), radius: 
        15).ignoresSafeArea()
        Button {
            print("Button was tapped")
        } label: {
            Image(systemName: "chevron.backward")
                .padding()
                .foregroundColor(.black)
          }.position(x: 40, y: 40)
         }.frame(width: 700, height: 300, alignment: .center)
         }
         }


 //Define the custom navigation using path drawing and make struct confirming to Shape 
struct CustomShapeNav: Shape {
   func path(in rect: CGRect) -> Path {
    Path { path in
        path.move(to: CGPoint(x: 0, y: 0)) // start point from 0 x-axis & 0 y-axis
        path.addLine(to: CGPoint(x: rect.maxX , y: 0)) // draw line max x-axis & 0 y-axis
        path.addLine(to: CGPoint(x: rect.maxX, y: 50)) // draw line max x-axis & 50 y-axis
        path.addLine(to: CGPoint(x: 0, y: 50))  // draw line 0 x-axis & 50 y-axis
        path.move(to: CGPoint(x: 0, y: 50)) // make the start point 0,50 instead of 0,0
        path.addQuadCurve(to: CGPoint(x: 90, y: 90), control: CGPoint(x: 
        0, y: 85)) // add curve to point 90,90 and make the point of curve 0,85
        path.addLine(to: CGPoint(x: rect.maxX - 90, y: 90))  // draw line -90 x-axis & 90 y-axis
        path.addQuadCurve(to: CGPoint(x: rect.maxX, y: 50), control: CGPoint(x: rect.maxX, y: 85))
        // add curve to point max x-axis and 50 and make the point of curve max x-axis and 85
        
        }
        }
        }

and the navigation bar will be like that view
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文