iOS UIView Draw(CGRect rect) never called when I set VerticalOptions="CenterAndExpand" - xamarin.ios

I am using an iOS UIView for Custom control in Xamarin.IOS. I have used this control in Xamarin.Forms Xaml.
If I set my Grid Horizontal and VerticalOptions="CenterAndExpand", My Draw() in UIView never gets called. It works fine if i set the value to 'FillAndExpand'.
My requirement is to place my custom control in center of my screen.
Is there any way to handle this? or is there other way I could display my UIView in Center of my screen.
<ScrollView>
<Grid HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<localControls:CustomUIView />
</Grid></ScrollView>
I need this Draw(CGRect rect) method to get the Width and Height of the available size in Screen. Hence my Draw is mandatory.

You can use AbsoluteLayout to let the view to the center of screen.
Views within an AbsoluteLayout are positioned using four values:
X – the x (horizontal) position of the view's anchor
Y – the y (vertical) position of the view's anchor
Width – the width of the view
Height – the height of the view
Each of those values can be set as a proportional value or an absolute value.
Values are specified as a combination of bounds and a flag. LayoutBounds is a Rectangle consisting of four values: x, y, width, height.
Code in Xaml like this:
<ContentPage.Content>
<AbsoluteLayout>
<local:MyCustomView AbsoluteLayout.LayoutBounds="0.5,0.5,300,200" AbsoluteLayout.LayoutFlags="PositionProportional" BackgroundColor="Red"/>
</AbsoluteLayout>
</ContentPage.Content>
AbsoluteLayout.LayoutBounds="0.5,0.5,300,200" AbsoluteLayout.LayoutFlags="PositionProportional" means the anchor is the center of the screen. And the view's Width and Height is 300 and 200.
It works like this:
BTW, I tried your code, but it works fine on my side with Visual Studio 15.3.5 and Xamarin.iOS 11.0.0.0, updating yours may solve the CenterAndExpand issue.

Related

How do I get the elementView size of UML classes using JointJS?

I'm implementing the class diagram where the users can change the class' name, attributes and methods. I'm having trouble with resizing the class rectangle width depending on name, attribute or method length. These can overflow and go outside the rectangle, and I just can't find a method to get the correct size.
I also have added an element tool to the elementView, and set its x and y to "100%". According to documentation: "Use percentage strings (e.g. '40%') to position the button relatively to the element width/height." but I guess this isn't the model's size, as I typed JSON.stringify(graph.toJSON()) and got the following.
JSON.stringify(graph.toJSON())
The size is the default value I set (260x100).
I checked the size of the attribute using DevTools:
Size of attribute
The gray button detects the width number I need (330), so I guess that the x in element tools is the elementView's width. I looked at the documentation and can't find any method like .size() or .getSize() for the elementView. I need to resize the rectangle to the size of Max(name length, attribute length, method length) + some aditional space to not look cramped up. I have seen some solutions like "Use [number of letters] * [some constant]" but that is no good as typing 5 "W"s doesn't take the same space as typing 5 "i"s.
So how do I get the width of the whole element (in my case 330 from text length + the three pixels from the left border of the rectangle to the first text letter)?
Okay, so after searching through several Google Groups chats I found some pieces of code that helped me.
For getting the width I needed, I used elementView.getBBox().width.
Where I didn't have the element view right at hand, I at least had the model ID, and so I used paper.findViewByModel(modelId) to get the element view.

Can't get buttons in custom layout to return a minimal width measurement with WRAP_CONTENT

I created a custom flow layout class to lay out a series of buttons of equal height and various widths depending on what text each button displays. Each row contains as many buttons as possible, and any remaining space is distributed evenly to fill up the row. This generally works, but I can't get the buttons to be just small enough to fit their text, and as a result most of the buttons are exactly the same size.
This is how I'm creating the button and adding it to the flow:
Button button = new Button(this);
button.setMinimumWidth(0);
button.setText(eventType.get("name").asString());
button.setAllCaps(false);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
button.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
eventTypeFlow.addView(button);
In the layout class' onLayout, I measure each button like this:
View child = getChildAt(childIndex);
measureChild(child, MeasureSpec.makeMeasureSpec(clientWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(clientHeight, MeasureSpec.AT_MOST));
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
But unless the text in the button is especially long, the width is always the same (264 in my case). I tried creating a linear layout XML with the button in it, setting minimum width to 0 and layout:width to wrap_content and that way the button is smaller, but when I distribute the extra space the linear layout grows, not the button.
Is there a way to get the buttons to return a smaller measurement when adding them directly to the custom layout?
Try using a LinearLayout using the layout_weight attribute. Give each button equal weight (1) and set your android:width attribute to 0dp.
Linear Layout layout_weight
More on LinearLayout's LayoutParams can be found here.
What ended up working is inflating the same linear layout XML I described in my question, then removing the button from the linear layout and adding it directly to my custom layout. Not very elegant, but it's the only thing I found that gets those buttons to be smaller.

Change UITextView height according to screen size

I am trying to change the height of my textview through code when the 4 inch screen is detected. Unfortunately I cant get my current code to work. Can anyone see what is wrong? Both ways its written it doesn't work. In the code I show both ways I've tried. I have this written in viewDidLoad method too.
If this cannot be worked out then can anyone help me out to set height attribute value in attribute inspector for both the screens (with height 568 and 480)
my code,
if ((int)[[UIScreen mainScreen] bounds].size.height==568)
{
[myTextView setFrame:CGRectMake(20.0f, 78.0f, 280.0f, 415.0f)];
}
uncheck 'use autolayout' from interface builder and use 'autosizing' from interface builder to resize your textview height

iPhone positioning a UIImageView with code? Sure it's simple to solve

This is very simple so I'm not sure why I can't do this. All I want to do it position some UIImageViews when my app becomes active. I had been using CGAffineTransformMakeTranslation but I can now see that this is not correct because it moves the view a set amount rather than moving it to a set position. What should I be using?
You could just change the frame of your UIImageView to:
set the x and y origin of the frame to the position you want your
UIImageView to be placed with respect to the containing view.
keep the width and height of your UIImageView the same
Something like this:
imageView.frame = CGRectMake([desired x coordinate], [desired y coordinate], imageView.frame.size.width, imageView.frame.size.height);

Dynamic tab width in a Flex 4 TabBar component with skin

I have a Flex 4 TabBar component with a custom skin. In this skin, the item renderer is defined with an ButtonBarButton with a custom skin.
All the tabs in the itemrenderer have now the same width.
The client however wants me to make the tabs width dynamically, so a smaller tab for a smaller label, a bigger one for a long label.
Does anybody know how to adjust the width of Tab (ButtonBarButton)?
Thanks a million!
I found something that works.
Create a custom component that inherits from ButtonBarButton, call it CustomTabButton.
Add a bindable property tabWidth.
Then when we update tabWidth, the width of the tab is adjusted with it.
THis is how you update the skin:
set the host component to the custom class CustomTabButton.
In the SparkSkin definition, bind the width value to the hostComponents property tabWidth.
width="{hostComponent.tabWidth}"
The skin looks like this:
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
minHeight="54"
width="{hostComponent.tabWidth}"
xmlns:tabbar="be.boulevart.project.components.tabbar.*">
<!-- host component -->
<fx:Metadata>
<![CDATA[
[HostComponent("be.boulevart.project.components.tabbar.CustomTabButton")]
]]>
</fx:Metadata>

Resources