Get width of Entry in Xamarin.Forms












1














How do I get the width of an Entry view in Xamarin.Forms? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath).



Custom Renderer:



void SetBorderWidth(Controls.Entry control)
{
Control.BorderStyle = UITextBorderStyle.None;
var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
myBox.BackgroundColor = control.BorderColor.ToUIColor();
Control.AddSubview(myBox);
}


Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. But if I put a number the line underneath suddenly shows.



Additionally when I print out the width and widthrequest they have a value of -1.










share|improve this question
























  • When are you calling SetBorderWidth?
    – SushiHangover
    Nov 13 '18 at 6:48










  • Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    – iamlawrencev
    Nov 13 '18 at 6:49
















1














How do I get the width of an Entry view in Xamarin.Forms? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath).



Custom Renderer:



void SetBorderWidth(Controls.Entry control)
{
Control.BorderStyle = UITextBorderStyle.None;
var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
myBox.BackgroundColor = control.BorderColor.ToUIColor();
Control.AddSubview(myBox);
}


Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. But if I put a number the line underneath suddenly shows.



Additionally when I print out the width and widthrequest they have a value of -1.










share|improve this question
























  • When are you calling SetBorderWidth?
    – SushiHangover
    Nov 13 '18 at 6:48










  • Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    – iamlawrencev
    Nov 13 '18 at 6:49














1












1








1







How do I get the width of an Entry view in Xamarin.Forms? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath).



Custom Renderer:



void SetBorderWidth(Controls.Entry control)
{
Control.BorderStyle = UITextBorderStyle.None;
var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
myBox.BackgroundColor = control.BorderColor.ToUIColor();
Control.AddSubview(myBox);
}


Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. But if I put a number the line underneath suddenly shows.



Additionally when I print out the width and widthrequest they have a value of -1.










share|improve this question















How do I get the width of an Entry view in Xamarin.Forms? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath).



Custom Renderer:



void SetBorderWidth(Controls.Entry control)
{
Control.BorderStyle = UITextBorderStyle.None;
var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
myBox.BackgroundColor = control.BorderColor.ToUIColor();
Control.AddSubview(myBox);
}


Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. But if I put a number the line underneath suddenly shows.



Additionally when I print out the width and widthrequest they have a value of -1.







c# xamarin xamarin.forms xamarin.ios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:09

























asked Nov 13 '18 at 6:25









iamlawrencev

636




636












  • When are you calling SetBorderWidth?
    – SushiHangover
    Nov 13 '18 at 6:48










  • Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    – iamlawrencev
    Nov 13 '18 at 6:49


















  • When are you calling SetBorderWidth?
    – SushiHangover
    Nov 13 '18 at 6:48










  • Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    – iamlawrencev
    Nov 13 '18 at 6:49
















When are you calling SetBorderWidth?
– SushiHangover
Nov 13 '18 at 6:48




When are you calling SetBorderWidth?
– SushiHangover
Nov 13 '18 at 6:48












Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
– iamlawrencev
Nov 13 '18 at 6:49




Inside protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
– iamlawrencev
Nov 13 '18 at 6:49












1 Answer
1






active

oldest

votes


















1














In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField, since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame.



Something like (I do not have my personal code with me, but this should be "close"):



UIView myBox;
public override void Draw(CGRect rect)
{
base.Draw(rect);
switch (myBox)
{
case null:
myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
Control.AddSubview(myBox);
break;
default:
myBox.Frame = new CGRect(0, 40, rect.Width, 1);
break;
}
}





share|improve this answer























  • Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
    – iamlawrencev
    Nov 13 '18 at 7:36










  • Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
    – SushiHangover
    Nov 13 '18 at 7:39












  • I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
    – iamlawrencev
    Nov 13 '18 at 7:52












  • @iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
    – SushiHangover
    Nov 13 '18 at 7:56













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275001%2fget-width-of-entry-in-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField, since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame.



Something like (I do not have my personal code with me, but this should be "close"):



UIView myBox;
public override void Draw(CGRect rect)
{
base.Draw(rect);
switch (myBox)
{
case null:
myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
Control.AddSubview(myBox);
break;
default:
myBox.Frame = new CGRect(0, 40, rect.Width, 1);
break;
}
}





share|improve this answer























  • Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
    – iamlawrencev
    Nov 13 '18 at 7:36










  • Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
    – SushiHangover
    Nov 13 '18 at 7:39












  • I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
    – iamlawrencev
    Nov 13 '18 at 7:52












  • @iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
    – SushiHangover
    Nov 13 '18 at 7:56


















1














In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField, since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame.



Something like (I do not have my personal code with me, but this should be "close"):



UIView myBox;
public override void Draw(CGRect rect)
{
base.Draw(rect);
switch (myBox)
{
case null:
myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
Control.AddSubview(myBox);
break;
default:
myBox.Frame = new CGRect(0, 40, rect.Width, 1);
break;
}
}





share|improve this answer























  • Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
    – iamlawrencev
    Nov 13 '18 at 7:36










  • Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
    – SushiHangover
    Nov 13 '18 at 7:39












  • I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
    – iamlawrencev
    Nov 13 '18 at 7:52












  • @iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
    – SushiHangover
    Nov 13 '18 at 7:56
















1












1








1






In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField, since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame.



Something like (I do not have my personal code with me, but this should be "close"):



UIView myBox;
public override void Draw(CGRect rect)
{
base.Draw(rect);
switch (myBox)
{
case null:
myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
Control.AddSubview(myBox);
break;
default:
myBox.Frame = new CGRect(0, 40, rect.Width, 1);
break;
}
}





share|improve this answer














In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField, since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame.



Something like (I do not have my personal code with me, but this should be "close"):



UIView myBox;
public override void Draw(CGRect rect)
{
base.Draw(rect);
switch (myBox)
{
case null:
myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
Control.AddSubview(myBox);
break;
default:
myBox.Frame = new CGRect(0, 40, rect.Width, 1);
break;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 7:13

























answered Nov 13 '18 at 7:07









SushiHangover

50.7k53886




50.7k53886












  • Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
    – iamlawrencev
    Nov 13 '18 at 7:36










  • Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
    – SushiHangover
    Nov 13 '18 at 7:39












  • I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
    – iamlawrencev
    Nov 13 '18 at 7:52












  • @iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
    – SushiHangover
    Nov 13 '18 at 7:56




















  • Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
    – iamlawrencev
    Nov 13 '18 at 7:36










  • Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
    – SushiHangover
    Nov 13 '18 at 7:39












  • I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
    – iamlawrencev
    Nov 13 '18 at 7:52












  • @iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
    – SushiHangover
    Nov 13 '18 at 7:56


















Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
– iamlawrencev
Nov 13 '18 at 7:36




Is there a way to get the width from the Xamarin.Forms.Entry element and not override the Draw method?
– iamlawrencev
Nov 13 '18 at 7:36












Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
– SushiHangover
Nov 13 '18 at 7:39






Why would you not want to use Draw? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In the OnElementChanged the width is not defined as the Form's layout engine has not completed and thus the native controls typically has a width/height of zero.
– SushiHangover
Nov 13 '18 at 7:39














I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
– iamlawrencev
Nov 13 '18 at 7:52






I would like to not override too much stuff and rely more on Xamarin.Forms if possible. I have already resolved this thanks to what you said that the native controls typically have a width/height of zero before the Form's layout engine completes its process. So I used the OnElementPropertyChanged method and got the width after it has been assigned a non-zero width by the Form's layout engine.
– iamlawrencev
Nov 13 '18 at 7:52














@iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56






@iamlawrencev OnElementPropertyChanged is just another override, same as Draw and slightly slower are you have to do a string comparison for the prop name.... but remember the OnElementPropertyChanged for Width can and will get called multiple times just like Draw so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275001%2fget-width-of-entry-in-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine