8.4 C
Canberra
Saturday, July 26, 2025

ios – SwiftUI – Including Button inside AutoCorrect Suggestion Bar


Anybody know if its doable so as to add a button to Auto Suggestion Bar? Ended up making an attempt the code under and I used to be in a position so as to add a button on prime of the keyboard. However confused if there’s an api that permits us to change autocorrect standing bar.

CustomTextField

import Basis
import UIKit
import SwiftUI

struct CustomTextfield: UIViewRepresentable {
    @Binding var textual content: String
    var keyType: UIKeyboardType
    
    func makeUIView(context: Context) -> UITextField {
        let textfield = UITextField()
        textfield.keyboardType = keyType
        let toolBar = UIToolbar(body: CGRect(x: 0, y: 0, width: textfield.body.measurement.width, peak: 44))
        let doneButton = UIBarButtonItem(title: "Accomplished", fashion: .carried out, goal: self, motion: #selector(textfield.doneButtonTapped(button:)))
        toolBar.objects = [doneButton]
        toolBar.setItems([doneButton], animated: true)
        textfield.leftView = toolBar
        return textfield
    }
    
    func updateUIView(_ uiView: UITextField, context: Context) {
        uiView.textual content = textual content
        
    }
}

extension  UITextField{
    @objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
        self.resignFirstResponder()
    }
    
}

TestView

import Basis
import SwiftUI

struct TestView : View {
    @State var textual content = ""
    
    var physique: some View {
        CustomTextfield(textual content: $textual content, keyType: UIKeyboardType.asciiCapable)
            .body(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                    .stroke(Shade.blue, lineWidth: 4)
        )
    }
}

enter image description here

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles