iOS Code Snippet Resize view with parent

SubName: autoresizingMask

Description:
Ios doesn't have the FillParent option that Android does, instead it has a more flexible approach using a mask:
Documentation is here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/autoresizingMask

The mask is a series of bits that defines how a subviews size should be adjusted when the parent size changes.

Possible options are:

UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5

We can access this using NativeObject:

B4X:
Dim INO As NativeObject = Img
INO.SetField("autoresizingMask",16)

Where 16 is equivelent to 1<<4 (Bit.ShiftLeft(1,4))

The code above will change the height of the view when the parents height changes. The change is relative, so if the view is set at 50% of the parents height, it will remain so.

To change height and width the code would be:

B4X:
Dim INO As NativeObject = Img
INO.SetField("autoresizingMask",18)

This function is contained in the UIView class so should work on any View.

Tags: FillParent, Resize View
 
Last edited:
Top