I have a button with a set background image. It's a png with most of the image being transparent. When that image is tapped on (selected), I would like to update the background image of the button to a different image. It seems to be working, but it's not removing the original background image. It seems to be placing the new background image behind the old one. So the transparent background image seems to be above the selected background image.
Setting the background image:
owedTotalBtn.setBackgroundImage( UIImage.init(named:"total-owed"), for: .normal)
Updating the background image one the button is tapped:
#IBAction func showOwed(_ sender: Any) {
owedTotalBtn.setBackgroundImage( UIImage.init(named:"total-owed-selected"), for: .normal)
}
You have set background Image for both normmal state. Try this code:
owedTotalBtn.setBackgroundImage( UIImage.init(named:"total-owed"), for: .normal)
owedTotalBtn.setBackgroundImage( UIImage.init(named:"total-owed"), for: .selected)
#IBAction func showOwed(_ sender: Any) {
sender.isSelected = !sender.isSelected
}
Related
I have 2 screens with which I am navigating between them. One screen is for clicking a button to navigate to the other screen and the other screen is for presenting some other information.
Screen one
I have the following piece of swift code since screen 2 uses third part library
let host = UIHostingController(rootView: AnyView(vc))
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let application = windowScene?.windows.first
ScanGuideContainer(title: self.viewModel.defaultLocalizer.stringForKey(key: ""), icon: icon,action: {
application?.rootViewController?.present(host, animated: true, completion: {
})
})
.environmentObject(viewModel)
This automatically launches a modal pop which then navigates to Screen2.
Screen 2
This screen has two options either to go back to the modal by pressing the back button or automatically go back once done processing some information which when it does so it returns a new value which then I can use to dismiss the host automatically since its a boolean value.
I can access the second screen viewController via
secondScreen?.viewController
How can i dismiss the modal from first screen when user presses back button of second screen?
What I tried
.onChange(of: self.ViewModel.dismissModal){ newvalue in
if(newvalue) {
host.dismiss(animated: true)
}
}
I set self.ViewModel.dismissModal value from second screen on but it does not work. please help
I use a TabView to simulate a horizontal scrolling paged list. This work fine. But I want to change the background, according to the currently active tab. I utilise .onAppear() for this, but this results in the images not being in sync with the item in the current tab (see video: https://youtube.com/shorts/aQxrErRmCi8?feature=share)
Basically, it should only change the background whenever the tab is 'fully' selected and centered on screen. Not as soon as the next or previous tab comes onto screen just a few pixels.
Secondly, I want to be able to set the selected programmatically, for instance after I insert a new object, I want to select the first tab. How Can I achieve that. The tabs have the id of my item (game) as tag.
Relevant code:
#State private var backgroundImage = AppImages.gameDefaultBackgroundImage
#ViewBuilder var tabList: some View {
TabView {
ForEach(gamesViewModel.games) { game in
GameCardView(game: game)
.tag(game.id)
.onAppear {
withAnimation(.easeInOut) {
backgroundImage = gamesViewModel.getGameImageCoreData(for: game)
}
}
.listRowBackground(Color.clear)
.frame(width: getScreenRectangle().width - 20)
.onTapGesture { showAnnotationsView(for: game) }
.onLongPressGesture(minimumDuration: 0.15) { gameDetailsView(for: game) }
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
}
If more code is needed, please ask ...
I am showing a UIViewController via a SwiftUI view, like so:
#available(iOS 13, *)
struct WelcomeNavView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<WelcomeNavView>) -> UINavigationController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVc = storyboard.instantiateViewController(withIdentifier: "welcomeNav") as! UINavigationController
return navVc
}
func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<WelcomeNavView>) {
}
}
I then present it from a ViewController like so:
self.viewController?.present(style: .fullScreen) {
WelcomeNavView()
}
However, it does not occupy the entire screen and the UIHostViewController color is showing at the top and bottom:
How can I change the color of the UIHostingViewController's view.. Or expand the View it is holding to occupy the entire screen?
Another simple and quick solution is, you can ignore safe area of your WelcomeNavView() while presenting.
example
iOS 13:
WelcomeNavView().edgesIgnoringSafeArea(.all)
iOS 14 and above:
WelcomeNavView().ignoresSafeArea()
Here is the solution,
I ran into similar problem, wherein I had to set hosting view controller's background clear.
Apparently, rootView (probably our SwiftUI View) of hostingVC and hostingVC.view are different.
so this would allow you to change to background color.
hostingVC.view.backgroundColor = .clear
Of course use any color in place of ".clear".
Just keep in mind to change color of SwiftUI view passed as rootView to hostingVC accordingly, mostly make that clear, so it won't be shown against above set color.
Your_SwiftUI_View.background(Color.clear)
I'm making an app with SwiftUI. I have an Image that has been loaded by the user. I want to show its dimensions in the UI. How do I get these dimensions from this image which is held inside this Image?
To be clear, I don't mean the view's size on-screen. I mean the number of pixels wide and high, which is encoded in the image file/format (PNG, JPG, etc). Before it's been scaled on the screen or anything. The conceptual image's dimensions.
You can load the Image using the uiImage initialiser. By having the image loaded through UIImage, you get access to its size:
struct ContentView : View {
#State private var img = UIImage(named: "myimage") ?? UIImage()
var body: some View {
VStack {
Image(uiImage: img)
Text("\(img.size.width) x \(img.size.height)")
}
}
}
I currently have several labels and buttons in a UIView. I am using Storyboards and I want to add an image to the background. I tried it this way:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.png")!)
But unfortunately, this does not look right in the larger screen sizes. I think that I need to add a UIImageView. When I added a UIImageView, I couldn't figure out how to set it to be in the background so my button and labels could still be seen.
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
return theImageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(someImageView) //This add it the view controller without constraints
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
someImageView.frame = view.bounds
}
In your storyboard:
1- Add a UIImageView (inside your main view and outside everything else) and set its image to the image you want.
2- setup its constraints properly to fill the screen (give it 0 from all sides).
3- From attribute inspector, set Content Mode property to Aspect Fit.
I solved this problem by going to the panel in Storyboards and moving the UIImageView above the container view that held the labels and buttons. The UIImageView remained inside of the View, but moved to the background (under the buttons and labels).