Segmentedcontrol text is not visible in iOS

Anonymous
2023-12-05T13:16:39.8833333+00:00

HI,

I am working on migrated Xamarin forms application to .net MAUI.

I am using segmented control and having renderer for iOS. the segmented control text is not visible in .net MAUI where as it it working fine in Xamarin application. I have tried in new MAUI app. Same behaviour observed.

Please help me on this.Simulator Screenshot - iPhone 14 Pro Max - 2023-12-05 at 18.45.37

Developer technologies .NET .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-12-08T09:25:35.38+00:00

    Hello,

    I can reproduce your issue, reusing renderer doesn't work with the code snippets you provide, please create a custom control using handlers, see Create custom controls with .NET MAUI handlers - .NET MAUI | Microsoft Learn

    And you can refer to the following code:

    Create a handler:

    public partial class SegmentedControlHandler
        {
            public static IPropertyMapper<SegmentedControl, SegmentedControlHandler> PropertyMapper = new PropertyMapper<SegmentedControl, SegmentedControlHandler>(ViewHandler.ViewMapper)
            {
               // Create the command mapper according to your control's bindable properties
            };
    
    
           public static CommandMapper<SegmentedControl, SegmentedControlHandler> CommandMapper = new(ViewCommandMapper)
            {
                
            };
    
    
           public SegmentedControlHandler() : base(PropertyMapper, CommandMapper)
            {
            }
            
        }
    
    
    #if IOS
        public partial class SegmentedControlHandler : ViewHandler<SegmentedControl, CustomUISegmentedControl>
        {
            protected override CustomUISegmentedControl CreatePlatformView()
            {
                return new CustomUISegmentedControl();
            }
    
    
           protected override void ConnectHandler(CustomUISegmentedControl platformView)
            {
                base.ConnectHandler(platformView);
    
               // Perform any control setup here
            }
    
    
           protected override void DisconnectHandler(CustomUISegmentedControl platformView)
            {
                platformView.Dispose();
                base.DisconnectHandler(platformView);
            }
    
    
       }
    #endif
    

    Create a native custom control derived from UISegmentedControl

    public class CustomUISegmentedControl: UISegmentedControl
    {
        public CustomUISegmentedControl()
        {// the following code is to simulate the setting title process
            for (var i = 0; i < 2; i++)
            {
                this.InsertSegment(i.ToString(), i, false);
    
           }
            var attr = new UIStringAttributes
            {
                ForegroundColor = UIColor.SystemPink// but the title always be black
            };
          this.SetTitleTextAttributes(attr, UIControlState.Selected | UIControlState.Normal | UIControlState.Highlighted);
        }
    }
    

    Register the handler in the MauiProgram class

    handlers.AddHandler(typeof(SegmentedControl), typeof(SegmentedControlHandler));
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.