Set Tab Bar image at runtime - swift

I wish to change the image of a tab bar item to a new image at runtime subject to a different view being updated.
Is there any way to achieve this?
I've struggled for hours now so any help would be massively appreciated.
Swift 4.2 Xcode 10.1
EDIT:
I've tried setting through self.tabBarController?.tabBar.items?[0].image in viewDidLoad in my VC. I've tried sub classing a TabBarController and setting through there, to which I couldn't get the runtime behaviour I was after.
To be more clear, i'm happy with my current selected and non selected image called "globe", but I want to listen out to a variable called "truth" and when truth is true I want to change the non selected image to "globe_red".
It is that behaviour of changing the image during the apps runtime that I am struggling with!

You can use following code to change image of tabBar.
self.tabBarController?.tabBar.items?[0].image = #imageLiteral(resourceName: "img")
or for selectedImage
self.tabBarController?.tabBar.items?[0].selectedImage = #imageLiteral(resourceName: "img")
Instead of 0 in items, you can use your index for selected tabBar.

Ok I fixed it!
Subclass UITabBarController
In viewDidLoad of your subclass set self.tabBar.items?[0].image and self.tabBar.items?[0].selectedImage to your desired image. Here I am settings the first VC's tab bar item but you can set any with the index or loop through and set all.

Related

Swift - Image color resets when Alert is displayed

So in my iOS app I have a menu designed like this:
The Images are created with the following code:
cell.imageCell?.image = menuItems[indexPath.row].image
cell.imageCell.image = cell.imageCell.image?.withRenderingMode(.alwaysTemplate)
cell.imageCell.tintColor = MenuTableViewController.fontColor
cell.imageCell.backgroundColor = UIColor.clear
This code is placed in the tableview cellForRowAt function. Now all the different viewcontrollers(HomeViewController, InfoViewController etc...) have no access at all to the menu controller and thus are not able to change the color of the image and nowhere else am I changing the color of these images. Now when I press one of the tabs that don't use any Alerts or modal views like home or info, the images stay perfectly fine, however when I press on Weather or excursions which download a json file with URLSession dataTask and display an alert telling to please wait, the images turn grey like the following:
I'm not sure how this is even possible that one viewcontroller can change another viewcontrollers subviews.
Thanks in advance
-Jorge
Perhaps what you see is a dimmed presentation of your icons. Don't use image templates or set tintAdjustmentMode to normal to stop fading icons. Usually that happens if you display alert or modal controller.
It looks like its the default UITableViewCell click style.
Try to set the style to none,
as answered here: How can I disable the UITableView selection highlighting?
Swift:
cell.selectionStyle = UITableViewCellSelectionStyle.none

Adding ViewController's View as subview to UIPageViewController in iOS 8 gives weird UINavigationBar while show/hide

I have been working on a project where I needed to show list of images with zoom / swipe feature as presented view modal. I created custom ImageViewer using ContainerView in which I have added UIPageViewController. And on demand, I added ImageViews over the UIPageViewcontroller's view. When user taps to imageView, the top UINavigation gets shown and hidden on toggle basis.
Everything worked as expected in iOS 7.1 and less. However when I tested the functionality in iOS 8 devices and simulator, the ImageViews were not added to UIPageViewController in TopLeft ( beneath UINavigationBar ). It is added below the NavigationBar as shown in bug_iOS_8.png below.
Once I touch the buggy view, it repositions itself to correct position as in image expected.png below.
I have created and tested the issue in sample project and it seems it is bug in iOS 8 itself with UIPageViewController. I went through couple of questions regarding weird behavior of UIPageViewController too. Please check the sample app here and kindly let me know if anyone has any hints on what is going on.
Thank you for your time and help.
PS: BTW I am using following version of Xcode.
Update 1:
I have tried as per the pin suggestions. However, the problem aligning is with the main view of UIViewController rather than its subview. In below image, Yellow is main View of UIViewController and red one is added subview, I added pin to "red" on in reference to superview "yellow" one. Please check following screen shots.
On startup.
After touch on screen.
Kind Regards,
check in storyboard for particular viewController and make sure Extended Edges >> Under top bars is not selected.
I had a very similar issue and the way i fixed it was select the view being added to the UIPageViewController in the storyboard and selected the pin options. Below in the image you can see the Constrain to margins options, make sure you unselect that. Also when choosing what view to pin it to make sure you select the superview and not the Top Layout Guide.
Curious are you hiding your nav controller like this?
[self.navigationController setNavigationBarHidden:YES];
also if you have multiple Nav controllers, you might need to check you are referencing the correct one.
If you want it to be hidden when you present the ViewController you should move it to
-(void) ViewWillAppear{
}

How to change the size of a view controller on the storyboard for editing purpose?

I have a simple question and I have spent countless hours trying to figure this out.
How can I change the size of a view controller in an Xcode Storyboard for editing purposes?
I have a large form/questionnaire I would like to place on my view controller, and I simply cannot work with the current size the storyboard gives me.
Furthermore, none of the provided sizes ("Inferred", "FreeForm", "Retina 3.5", and "Retina 4") are big enough!
Does anyone have a solution to my problem?
Xcode 5.x+ (including 6.x and 7.x)
From Xcode 5 and beyond, there's a even easier solution. Inside the UIStoryboard, you can simply select your UIViewController and go directly to the fifth tab. Just change the Simulated Size from Fixed to Freeform . After that, you should see the Width and Height fields, as the image below:
Xcode 4.x (still valid for newer ones)
Select your view controller, and in the fourth tab of InterfaceBuilder, select 'freeform' in size.
After freeform is selected, you now are able to change the size of the main view inside your UIViewController. Just select the main view of the controller and change its frame in the fifth tab, like this:
To easily switch between various iOS device sizes, use a Simulated Size Metric:
Attributes Inspector > Simulated Metrics > Size:
click on 'View Controller' option on the top of the displayed storyboard and select proper size in Attributes Inspector->Simulated Metrics
You Can Change your view controller from selecting view controller, then go to file inspector.There you will see the checkbox "Use size classes". By unchecking it, you can change the size of view controller .......
Note: for the first time using of xcode

iPhone - Custom Tab Icons, Remove Highlight

I am creating a custom tab bar for my iPhone app and I need to change the images. I have changed the actual tab bar background, but I need to know how to add custom images for the icons and their respective "selected" icons. I also need to remove the square highlight that is default. Pretty much, it just needs to be my icons. Also wondering if the images can be coloured or not. I've looked a lot of this, but no one seems to have the solution.
Someone please help.
hide the inbuilt tabBar view
create your own custom UIView (or UIScrollView in case if you need more number of tabs) with the frame of inbuilt tabBar view and fill up, especially with array of UIButtons & then add your custom view as a subview of the TabBarController.
Play with the UIButton's background image (which is your tab icon image) and its addTarget:action:forControlEvents: in order to set the appropriate selectedIndex value of the TabBarController, i.e. set the selectedIndex according to the button's position in the custom view.
This is how I've implemented :)
No. You cannot customise the tab bar icons in the UITabBar.
They are designed to be used with an alpha masked image.
Someone discovered a way to hack the colours though here: Custom colors in UITabBar

How to switch views by buttons on iPhone?

I want to switch 3 views and let them switch from 1-2-3. The first view is to let users input name and password, the second view will show his information to let him confirm and the third view will show him a list of options so he can choose what to do next.
The problem is how to switch these views? If I use a navigation controll to switch views, how can I add textfield in it so users can input their infomation?
Thanks for your answers and I find this isn't so complicated as I thought earlier. After a few attempts, I finally made it. I just use NavigationController to switch between UIViews.
Now I understand that navigation controller can swith not only from UITableView to UIView, but also from UIView to UIView. Using a UIView class, I can organize all the controls easily in IB.
Check out Apple's sample TableViewSuite application for hints. You'll find more sample code at Apple's iPhone Developer site.
You hate to write your code into
-(void)IBAction
set the button property by using connection Inspector set the button to touchesDown.
After this the view which you want to see at the beginning. Make it's alpha value 1 and other view's alpha value should be 0. After filling all information when you will click on button. Make alpha value of first view should be 0 and the second view's alpha should be 1. Like that you can proceed