Bug 38018 - [iOS] UIControlState.Selected seems to be ignored when trying to set text attributes for that control state in a custom TabbedPageRenderer.
Summary: [iOS] UIControlState.Selected seems to be ignored when trying to set text att...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.0.0
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-26 01:20 UTC by Jon Goldberger [MSFT]
Modified: 2017-03-02 19:16 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project (401.10 KB, application/zip)
2016-01-26 01:20 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jon Goldberger [MSFT] 2016-01-26 01:20:07 UTC
Created attachment 14726 [details]
Test Project

## Description

Setting this is an enhancement request for now, but feel free to change if there may be a bug that is causing this issue. 

Customer wrote in because they were trying to set the font size to be different when a tab in a TabbedPage is the selected tab. Code customer was using in the renderer to try to achieve this was:

>public class ExtendedTabbedPageRenderer : TabbedRenderer
>{
>protected override void OnElementChanged(VisualElementChangedEventArgs e)
>{
>base.OnElementChanged(e);
>
>// Set Text Font for unselected tab states
>UITextAttributes normalTextAttributes = new UITextAttributes();
>normalTextAttributes.Font = UIFont.FromName("American Typewriter", 30.0F);
>
>UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
>UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Disabled);
>UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Reserved);
>UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Application);
>
>// Selected tab states
>UITextAttributes selectedTextAttributes = new UITextAttributes();
>selectedTextAttributes.Font = UIFont.FromName("American Typewriter", 10.0F);
>UITabBarItem.Appearance.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Selected);
>UITabBarItem.Appearance.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Focused);
>UITabBarItem.Appearance.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Highlighted);
>}
>}

but this only worked to change the font. The size remained constant regardless of whether the tab was selected or not. Looking over some iOS resources, it seems that it is necessary to also change each child view controller's TitleTextAttributes in the SelectedViewController property override. I did the following which worked to achieve the desired result. It seems you just have to set the UIControlState.Normal font and size to the desired values in the SelectedViewController property override as follows:

> protected override void OnElementChanged (VisualElementChangedEventArgs e)
>{
>	base.OnElementChanged (e);
>
>	// Set Text Font for unselected tab states
>	UITextAttributes normalTextAttributes = new UITextAttributes();
>	normalTextAttributes.Font = UIFont.FromName("American Typewriter", 30.0F);
>
>	UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
>}
>
>public override UIViewController SelectedViewController {
>	get {
>		UITextAttributes selectedTextAttributes = new UITextAttributes();
>		selectedTextAttributes.Font = UIFont.FromName("American Typewriter", 10.0F);
>		if (base.SelectedViewController != null)
>			base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
>
>		return base.SelectedViewController;
>	}
>	set {
>		base.SelectedViewController = value;
>
>		foreach (UIViewController viewController in base.ViewControllers) {
>			UITextAttributes normalTextAttributes = new UITextAttributes();
>			normalTextAttributes.Font = UIFont.FromName("American Typewriter", 30.0F);
>
>			viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
>		}
>	}
>}

Test project with original non-working code attached. Working code is included but commented out. To get working version, comment out lines 17-36 in ExtendedTabbedPageRenderer.cs and uncomment lines 40-70.


## Steps to reproduce.

1. Open the attached test project and launch iOS project to a simulator or device. 

2. Select a tab.

Expected result: font size for selected tab will be smaller.

Actual result: no change in font size for selected tabs. All tabs always show same font size.

## Notes

As I said, this is more of an enhancement request, but if there is a bug in forms that is blocking using the UIControlState.Selected as in the original code provided by the customer, then please change to normal (non-enhancement) bug report. However ultimately it seems like this is something that would be nice to be able to do without a custom renderer at all (enhancement request).


## Environment

Forms version 2.0.1.6495

=== Xamarin Studio ===

Version 5.10.1 (build 6)
Installation UUID: 964c531b-d928-456b-a9ae-e1f82266b360
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Version: 0.22.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.0.0 (Business Edition)
Hash: 7322991
Branch: master
Build date: 2015-12-08 16:20:29-0500

=== Xamarin.Mac ===

Version: 2.4.0.109 (Business Edition)

=== Xamarin Inspector ===

Version: 0.3.2.3
Hash: 1b526e6
Branch: master
Build date: Tue Nov 17 20:54:30 UTC 2015

=== Build Information ===

Release ID: 510010006
Git revision: 0b60eecdb531933734519c13257d16a780274aab
Build date: 2015-12-04 20:28:20-05
Xamarin addins: 9876fd7c9837977178411ec7375b4352c0a0d6af
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.2
Darwin Jons-iMac.local 15.2.0 Darwin Kernel Version 15.2.0
    Fri Nov 13 19:56:56 PST 2015
    root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64
Comment 1 Jon Goldberger [MSFT] 2016-01-26 01:28:03 UTC
Additional note:

It seems that even in a fully native iOS app, you need to do more than set the Appearance proxy to get this functionality. See:
http://stackoverflow.com/questions/25234671/changing-selected-tabbaritem-font-ios

But even so trying to follow the recommendation of the accepted answer above in the custom renderer did not work as expected either. It took a bit of playing around for me to come up with the workaround I found.
Comment 2 Paul DiPietro [MSFT] 2017-03-02 19:16:41 UTC
I just tested the reproduction against the nightly and this appears to have been fixed at some point; the selected tabs are changing font size.