Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom shaped (inverted T) bordered Uiview in iOS

I have to create a custom shaped (inverted T) bordered Uiview on iOS. I am attaching the screenshot below. I have researched a lot and I found a way using UIBezierPath from here.

But I didn't get any idea to shape my view as inverted T shaped.

Inverted bordered T

like image 200
Shamsudheen TK Avatar asked Dec 11 '22 23:12

Shamsudheen TK


2 Answers

UIViews are always rectangular. From the documentation:

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area.

Even though the view is limited to being rectangular, you can shape your active area in any way that you like. By combining that with a transparent background, you can imitate a view of any shape that you can draw.

When your rectangular view receives touches and other events, your event handlers should first check if the activity has happened in the inverted-T area. If the activity is outside, the event should be ignored.

like image 71
Sergey Kalinichenko Avatar answered Dec 26 '22 12:12

Sergey Kalinichenko


Phew.. Finally I have done it. I have used two UiViews subclasses (top & bottom).

The main challenge I faced was about the border, because if I set the border to my two views (top & bottom), it will not show as a single container item. :)

Steps that I done:

Created two UiView subclasses. Lets call topView and bottomView.

    TopView *topView = [[TopView alloc] initWithFrame:CGRectMake(220, 60, 200, 200)];
    [topView setBackgroundColor:[UIColor yellowColor]]; 
    [self.view addSubview:topView]; 

    BottomView *bottomView = [[BottomView alloc] initWithFrame:CGRectMake(130, 260, 380, 200)];
    [bottomView setBackgroundColor:[UIColor yellowColor]];
    bottomView.customShape = topView; //Set the custom shape as TopView to frame to draw the border.    
    [self.view addSubview:topView];

I have drawn three borders (top,right,left) for TopView and two full borders (bottom, right), two partial borders (top left, top right) for BottomView through overriding the drawRect method.

See my TopView class here.

See my BottomView class here.

Thanks to all.

Output:

Custom UiView

like image 45
Shamsudheen TK Avatar answered Dec 26 '22 11:12

Shamsudheen TK