Catalin Halic Catalin Halic - 3 months ago 42
C# Question

UILabel change position in center of the screen for iOS in Xamarin C#

I want to change the label position from the top of the screen to a center of the screen programmatically when I press a button.

How do I achieve that?

My Code so far:

outputMessageLabel.Frame = new CGRect(outputMessageLabel.Bounds.X,
UIScreen.MainScreen.Bounds.Size.Height / 2, outputMessageLabel.Bounds.Width,
outputMessageLabel.Bounds.Height);

Answer

There are two ways of doing this task:

  1. You can set two labels, one at top of screen and second at center. Hide the center one. On the action of the button, hide the top one and unhide the middle one (eg. mylabel.hidden = False).

  2. Other thing you can do is create the label programmatically in viewdidload or viewwillappear by putting:

    if (flag == 0){
        label location at top
    } else {
       label location at middle
    }
    

And on the button click, call your method viewdidload / viewwillappear through NSPostNotification.

Comments