Get width of Entry in Xamarin.Forms
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
add a comment |
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
When are you callingSetBorderWidth
?
– SushiHangover
Nov 13 '18 at 6:48
Insideprotected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
– iamlawrencev
Nov 13 '18 at 6:49
add a comment |
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
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
c# xamarin xamarin.forms xamarin.ios
edited Nov 13 '18 at 7:09
asked Nov 13 '18 at 6:25
iamlawrencev
636
636
When are you callingSetBorderWidth
?
– SushiHangover
Nov 13 '18 at 6:48
Insideprotected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
– iamlawrencev
Nov 13 '18 at 6:49
add a comment |
When are you callingSetBorderWidth
?
– SushiHangover
Nov 13 '18 at 6:48
Insideprotected 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
add a comment |
1 Answer
1
active
oldest
votes
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;
}
}
Is there a way to get the width from theXamarin.Forms.Entry
element and not override theDraw
method?
– iamlawrencev
Nov 13 '18 at 7:36
Why would you not want to useDraw
? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In theOnElementChanged
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 theOnElementPropertyChanged
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 forWidth
can and will get called multiple times just likeDraw
so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
}
Is there a way to get the width from theXamarin.Forms.Entry
element and not override theDraw
method?
– iamlawrencev
Nov 13 '18 at 7:36
Why would you not want to useDraw
? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In theOnElementChanged
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 theOnElementPropertyChanged
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 forWidth
can and will get called multiple times just likeDraw
so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56
add a comment |
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;
}
}
Is there a way to get the width from theXamarin.Forms.Entry
element and not override theDraw
method?
– iamlawrencev
Nov 13 '18 at 7:36
Why would you not want to useDraw
? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In theOnElementChanged
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 theOnElementPropertyChanged
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 forWidth
can and will get called multiple times just likeDraw
so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56
add a comment |
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;
}
}
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;
}
}
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 theXamarin.Forms.Entry
element and not override theDraw
method?
– iamlawrencev
Nov 13 '18 at 7:36
Why would you not want to useDraw
? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In theOnElementChanged
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 theOnElementPropertyChanged
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 forWidth
can and will get called multiple times just likeDraw
so make sure you are not just creating a new UIView every time....
– SushiHangover
Nov 13 '18 at 7:56
add a comment |
Is there a way to get the width from theXamarin.Forms.Entry
element and not override theDraw
method?
– iamlawrencev
Nov 13 '18 at 7:36
Why would you not want to useDraw
? This is the native draw routine of any UIView and the CGRect provided is the most current Frame size. In theOnElementChanged
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 theOnElementPropertyChanged
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 forWidth
can and will get called multiple times just likeDraw
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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