András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Tag: maui ios

  • Resizing the screen when an Entry gets focused in .NET MAUI

    This content has 7 months. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    When an entry field gets focused, the software keyboard appears, potentially covering the entry field. To provide a better user experience, you need to adjust the screen layout so that the entry field remains visible.

    First, create a parent class for your views, deriving from ContentPage, and add a HaveKeyboardOffsetProperty.

    public class ViewBase : ContentPage
    {
        public static readonly BindableProperty HasKeyboardOffsetProperty =
            BindableProperty.Create(nameof(HasKeyboardOffset), typeof(bool), typeof(ViewBase), false);
    
        public bool HasKeyboardOffset
        {
            get => (bool)GetValue(HasKeyboardOffsetProperty);
            set => SetValue(HasKeyboardOffsetProperty, value);
        }
    }
    
    

    iOS Solution:

    Next, create the PageElementMapper class to handle the keyboard appearance and adjust the screen layout.

    public class PageElementMapper
    {
        private static double _contentOriginalHeight;
        private static Thickness _contentOriginalMargin;
    
        public static void Map(IElementHandler handler, IElement view)
        {
            if (view is ViewBase viewData)
            {
                UIKeyboard.Notifications.ObserveWillShow((sender, args) =>
                {
                    if (viewData.HasKeyboardOffset)
                    {
                        _contentOriginalHeight = viewData.Content.Height;
                        _contentOriginalMargin = viewData.Content.Margin;
                        viewData.Content.HeightRequest = _contentOriginalHeight - args.FrameEnd.Height;
                        viewData.Content.Margin = new Thickness(0, args.FrameEnd.Height, 0, 0);
                    }
                });
    
                UIKeyboard.Notifications.ObserveWillHide((sender, args) =>
                {
                    if (viewData.HasKeyboardOffset)
                    {
                        viewData.Content.HeightRequest = _contentOriginalHeight;
                        viewData.Content.Margin = _contentOriginalMargin;
                    }
                });
            }
        }
    }
    
    

    Finally, register the mapper in the MauiProgram.cs file.

    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureMauiHandlers(handlers =>
                {
                    Microsoft.Maui.Handlers.PageHandler.ElementMapper.AppendToMapping("KeyboardOffset", (handler, view) =>
                    {
                        if (view is ViewBase)
                        {
    #if IOS
                            PageElementMapper.Map(handler, view);
    #endif
                        }
                    });
                });
    
            return builder.Build();
        }
    }
    
    

    Android solution:

    On Android, you can use a PropertyChanged method of the HasKeyboardOffset:

        private static void OnHasKeyboardOffsetPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
    #if ANDROID
            if (bindable is ViewBase view)
            {
                if (newValue is bool hasOffset)
                    if (hasOffset == true)
                        Microsoft.Maui.Controls.Application.Current.Dispatcher.Dispatch(() =>
                        {
                            Platform.CurrentActivity?.Window?.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
                        });
                    else
                        Microsoft.Maui.Controls.Application.Current.Dispatcher.Dispatch(() =>
                        {
                            Platform.CurrentActivity?.Window?.SetSoftInputMode(Android.Views.SoftInput.StateUnspecified);
                        });
            }
    #endif
        }
    

    Remarks

    The provided solution offers a way to manage this in .NET MAUI on iOS. However, always be open to improvements and share your solutions with the community for better practices. This might not be the best solution to do it.

  • .NET MAUI Hide software keyboard when tapping out of an Entry on iOS

    This content has 7 months. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    To hide the software keyboard when the user taps outside the entry field, set the HideSoftInputOnTapped property to True in your ContentPage definition.

    &lt;ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="MyMauiApp.MainPage"
                 HideSoftInputOnTapped="True">
    
        &lt;StackLayout Padding="10">
            &lt;Entry Placeholder="Tap here to enter text" />
        &lt;/StackLayout>
    &lt;/ContentPage>
    
    

    Please note that the HideSoftInputOnTapped property might not work as expected when tapping on certain controls like ScrollView. In such cases, you might need to implement a custom behavior to handle keyboard dismissal.

  • .NET MAUI iOS – Azure Pipelines error: ‘x’ is not available in iOS 16

    This content has 1 year. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    The Error: The error message suggests that the ‘UIKit.UISceneSessionActivationRequest’ type, used as a parameter in ‘UIKit.UIApplication.ActivateSceneSession,’ is not available in iOS 16.2 and was introduced in iOS 17.0. This discrepancy indicates a version misalignment in the development environment, specifically with the iOS SDK and Xcode.

    Root cause

    The root cause of this error lies in the version of the macOS image used in the Azure Pipelines configuration. By default, the ‘macOS-latest’ image is pulled, which corresponds to macOS 12 (at the time of the blog post). However, the .NET MAUI app with Azure Pipelines requires macOS v13 to work seamlessly, as it aligns with the necessary dependencies for iOS development.

    Resolution

    To resolve this error, developers need to update the macOS image specified in the Azure Pipelines configuration. Instead of using ‘macOS-latest,’ the configuration should be modified to use ‘macOS-13.’ This ensures that the appropriate version of macOS is utilized during the build process, addressing the compatibility issues with iOS 16.2 and the required UIKit types.

    Step-by-Step

    -Open your Azure Pipelines configuration file (typically named azure-pipelines.yml).
    -Locate the section where the macOS image is specified. It might look something like this:

    pool:
      vmImage: 'macOS-latest'
    

    -Update the image reference to ‘macOS-13’:

    pool:
      vmImage: 'macOS-13'
    

    -Save the changes to the configuration file.
    -Commit the updated configuration file to your version control system (e.g., Git).
    -Trigger a new build in Azure Pipelines, and the updated macOS image will be used.

  • .NET MAUI + Visual Studio Code: Debugging Cancelled: XCode Not Found

    This content has 1 year. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    One common issue users face is the “Debugging Cancelled: XCode Not Found” error on macOS. In this blog post, we’ll explore a step-by-step guide to troubleshoot and resolve this vexing problem.

    Solution 1: Verify VS Code Command Line Tools Installation
    Before diving into complex solutions, let’s start with the basics. Ensure that the VS Code command line tools are correctly installed on your machine. Run the following command in the terminal:

    xcode-select --install
    

    This command installs the necessary tools for XCode. After installation, verify that the path is correctly set by running:

    xcode-select -p
    

    Ensure that the path points to your XCode installation. If not, set it using the following command:

    sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
    

    Solution 2: Force Quit and Relaunch VS Code
    Sometimes, issues can be resolved by simply force quitting VS Code and relaunching it. This action ensures a fresh start, eliminating any temporary glitches that might be causing the problem.

    Solution 3: Restart VS Code
    A restart can work wonders in resolving software-related issues. Save your work, close VS Code, and then relaunch it. This simple step can refresh the IDE and might solve the “Debugging Cancelled: XCode Not Found” issue.

    Solution 4: Reinstall .NET MAUI Extension
    If the problem persists, the next step is to reinstall the .NET MAUI extension. Extensions can occasionally become corrupted or outdated, leading to compatibility issues. Open the Extensions view in VS Code, locate the .NET MAUI extension, and uninstall it. Afterward, reinstall the latest version from the Visual Studio Code marketplace.

    Solution 5: Reinstall Visual Studio Code
    If all else fails, consider reinstalling Visual Studio Code. Uninstall the current version, download the latest version from the official website, and perform a clean installation. This ensures that any corrupted files or configurations are completely removed, and you start with a fresh setup.