skip to Main Content

What is the way to manage this tab bar distribution, I want to add a image next to back button and in the center of the content add a text

My code:

// Create the label
let label = UILabel()
label.text = "Haz tu pedido"
label.sizeToFit()
label.textAlignment = NSTextAlignment.center
label.medium(size: 20, fontFamily: .roboto, color: .white)
label.frame.origin.y = 0

// Create the image view
let image = UIImageView()
image.image = UIImage(named: "IconSmall.png")
let imageAspect = image.image!.size.width/image.image!.size.height
image.frame = CGRect(x: label.frame.origin.x-label.frame.size.height*imageAspect , y: label.frame.origin.y, width: label.frame.size.height*imageAspect, height: label.frame.size.height)

image.contentMode = UIView.ContentMode.scaleAspectFit

let stack = UIStackView(arrangedSubviews: [image,label])
stack.axis = .horizontal

self.tabBarController?.navigationItem.titleView = stack

2

Answers


  1. use. leftBarButtonItem. &. title

     self.tabBarController?.navigationItem.title = "Haz tu pedido"
     self.tabBarController?.navigationItem.leftBarButtonItem?.image = UIImage(named: "IconSmall.png")
    
    
    Login or Signup to reply.
  2. You can modify the Back button image and style using UIkit and add a leading icon so it looks like what you need, do note that modifiyng the navigation bar will modify it in all the App.

    import SwiftUI
    
            struct backbutton: View {
             
                init() {
                    let appearance = UINavigationBarAppearance()
    
                    appearance.configureWithTransparentBackground()
                    appearance.shadowColor = .clear
                    appearance.backgroundColor = .clear
                    appearance.backgroundEffect = nil
    
                    //Button
                    let buttonAppearance = UIBarButtonItemAppearance(
                        style: .plain
                    )
    
                    buttonAppearance.normal.titleTextAttributes = [
                        .foregroundColor: UIColor.blue.withAlphaComponent(0)//
                    ]
    
                    let backimage = UIImage(
                        systemName: "pencil"
                    )?.withTintColor(
                        UIColor.red,
                        renderingMode: .alwaysOriginal
                    )
                    appearance.backButtonAppearance = buttonAppearance
                    appearance.setBackIndicatorImage(
                        backimage,
                        transitionMaskImage: backimage
                    )
    
                    UINavigationBar.appearance().standardAppearance = appearance
                    UINavigationBar.appearance().compactAppearance = appearance
                    UINavigationBar.appearance().scrollEdgeAppearance = appearance
                }
                
                var body: some View {
                    
                    NavigationStack {
                        NavigationLink(destination: secondView()) {
                            Text("Navigate")
                        }
                        .navigationTitle("This is my first view")
                    }
                }
            }
    
            struct secondView: View {
                
                @Environment(.presentationMode) var presentationMode
                
                var body: some View {
                    Text("Second view")
                        .navigationTitle("This is my second view")
                        .navigationBarItems(leading: BackButton(presentationMode: _presentationMode, foregroundColor: .red))
                }
                
            }
    
            struct BackButton: View {
                
                @Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
                var foregroundColor: Color
                
                var body: some View {
                    Button(action: { presentationMode.wrappedValue.dismiss()}) {
                        HStack {
                            
                            Image(systemName: "square.and.arrow.up")
                                .foregroundColor(.red)
                                .aspectRatio(contentMode: .fit)
                        }
                    }
                }
            }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search