When creating custom views, I generally create an initialization function by overriding the init(frame) function. When creating the view in my ViewController I initialize the view by passing in the .zero frame, and then set the layout using Material's layout function.
let descView = DescriptionView(frame: .zero)
However, this makes it so that my views in my custom view are not resized. For example, when setting up a custom text view in my custom view, it does not show up because I prepare it as so:
self.layout(descriptionTextView).width(self.width).height(self.height).centerVertically().centerHorizontally()
I assume it doesn't show up because when I initialize the view, its width and height are zero. What can I do to get around this?
Related
I am creating a UIViewController which have an animation on it and I want it to be displayed while "loading" or changing views. Is there a way so I can show that view in top of the others while they're doing their business logic and when they're done call in their respectives ViewDidLoad functions, a call to "hide" this facade view?
I think you want to display loading overlay which has your own custom animation and you want to show/hide this loading overlay on top of any View or ViewController. If my understanding is correct then you can achieve this as follows:
Suppose loadingOverlay is your animation view instance.
Assign some unique Tag to this:
loadingOverlay.Tag = 1678;
This can be any number which uniquely identifies your loading overlay view from all other views from UIWindow in any ViewController.
Show:
UIWindow myKeyWindow = UIApplication.SharedApplication.KeyWindow;
myKeyWindow.AddSubView(loadingOverlay);
Hide:
var loadingOverlay = myKeyWindow.ViewWithTag(1678);
//Remove completely
loadingOverlay?.RemoveFromSuperView();
//OR
//Simply hide it
loadingOverlay?.Hidden = true;
Note: If you want to use Hidden property, then you have to make sure that you add this view into UIWindow only once.
So, ideally, anything which you want to display on top of anything,
should go into KeyWindow (UiWindow)
Hope this helps!!
I want to add a new View to a Layout dynamically by layout.addView(view) method. The new View is not supposed to be visible, but I want it's Display List to be created so it doesn't have to redraw (call onDraw method) itself when I decide to show it (by animation, for example fade in).
I run this code:
customView = new CustomView(this.getContext());
customView .setAlpha(0.0f);
this.addView(customView ); // 'this' is RelativeLayout instance in this case
onDraw method gets called for customView and everything is fine. However, when I change anything in my layout (push a button, scroll, anything that invalidates layout), the onDraw method for my customView is called second time. After that, it isn't called any more if I don't invalidate my customView (correct behaviour).
I don't know why Android behaves this way. I want it to create customView, call onDraw, create a Display List for it, and not call onDraw any more until I invalidate my view. To sum up, onDraw is supposed to be called once.
And it has nothing to do with initial invisibility of customView, if alpha is set to 0.5f, behaviour is the same.
My question is, how to make Android call onDraw only once?
And if Android really has to call onDraw twice, then what should I do to enforce it to do it in some code right after this.addView(view); without setting up any timers because THAT would be totally ugly.
The behavior you are describing is ok and is a part of the android framework for view object -
Drawing
Drawing is handled by walking the tree and rendering each view that
intersects the invalid region. Because the tree is traversed in-order,
this means that parents will draw before (i.e., behind) their
children, with siblings drawn in the order they appear in the tree. If
you set a background drawable for a View, then the View will draw it
for you before calling back to its onDraw() method. Note that the
framework will not draw views that are not in the invalid region. To
force a view to draw, call invalidate()
(Taken from the official google android API docs).
Meaning your custom view is contained by the same view that contains the button\scrollbar etc. If you don't want it to be rendered everytime the onDraw method is called for the subtree your view resides in you can set the view's boolean flag to false - use setWillNotDraw() to do that. (you should place it on the activity's onCreate in order to render the view set this flag to false (which is also the default) and use invalidate() whenever you want to render the view).You can read the official google docs for further information.
How to create an custom ListView? I have Adapter, but I don't understand how to create an View like ListView of the Facebook, or App for SMS native from device... I'm needing background for TextView, this background, will have an arrow pointing left or right, I tried to create a View using canvas, but it is very difficult...
Every line in the listView of a layout which can be as complex as you want.Just you need an extra xml file for managing you custom layout (it should be contains widgets,fields,images etc as your design ) and the adapter would inflate this layout file for each row in its getView () method and assign the data to the individual views in the row .
I have an application that use the MVVM pattern. In the main view model, I create a view and its viewmodel of another control.
I need to know the height of the control, but when I create the control, if I see the Height value of the control I get NaN.
I use this code in my main view model:
miSecondControl = new mySecondControl(param1, param2);
double myHeight = miSecondControl.height;
This secondary control does not use the MVVM patter but it has code behind, because I only want to use it to print a fixed document, so I think that in this case MVVM would make more complex the solution.
I need the heigh property of the control to know if I need to create a new page because I don't have space in the actual page.
Thanks.
Height/Width are values that are specified at design/layout time.
Use FrameworkElement.ActualHeight and FrameworkElement.ActualWidth to get the rendered Height/Width of a control.
I would like to make a layout with a sidebar that can have widgets from different modules. Lets say there shall always be a login widget at the top if the user isn't logged in then it shall show user info. The getting started album guide could use it to display the latest albums and so on, i hope you understand how i want to use the sidebar.
Could it be done with a config file in autoload and a small code that read that config and calls the widgets on every page load?
There are several ways of page composition in Zend Framework 2:
1. Switching between Layouts
By default, ZF2 provides you with a single layout template layout.phtml.
In real-life applications, you will probably need to have several layouts
and switch the layout for certain controller/action. In each of your layouts, you will be able to show different widgets/sidebars.
2. Partial Views
A partial view is a .phtml view template file which can be rendered by another
view template. Partial views allow to compose your page of pieces and reuse pieces
of view rendering logic across different view templates. This is accomplished through the Partial view helper.
3. Placeholder View Helper
The Placeholder is another useful view helper allowing for capturing HTML
content and storing it for later use. Thus, analogous to the Partial
view helper, it allows to compose your page of several pieces.
4. Forward Controller Plugin
With the Forward controller plugin, you are able to call an action (for example, the action rendering some widget) from another module/controller from your controller and grab the output of that action. Then you are able to incorporate that output into your page.
5. Use View Models for Page Composition
When you write action methods for the controller classes, you use the ViewModel
class as a variable container for passing the variables from controller to view template,
and for overriding the default view template name. But, actually the ViewModel class is more than just a variable container plus view template name. In fact, it is closely related to the layout and page composition.
The third big capability of the view model class is that it allows for combining several
view models in a tree-like structure. Each view model in the tree has the associated
view template name and data variables that can be passed to the view template to control
the process of rendering.
This feature is internally used by Zend Framework 2 when "combining" the layout view template and the view template associated with the controller's action method. ZF2 internally creates the view model for the layout template and assigns it with layout/layout view template name. When your controller's action method returns the ViewModel object, this object is attached as a child to the layout view model.
So, you can attach your own view models to the default view model to render the page of pieces that you want.