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.