Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add padding to a UIView?

I could not find a way to do this with UIView, as I depict in the following picture: It portrays having a image background with a shadow. Is there anything of the shelf for padding, or do I need to add more child objects to achieve this effect?

enter image description here

like image 357
Rui Nunes Avatar asked Sep 05 '25 20:09

Rui Nunes


1 Answers

I think this would be pretty straightforward to do, but you'll have to implement it yourself.

If I understand your question correctly you'd like to have a padding value added automatically to your subviews - for example, pass an X/Y value of 0, 0 for a button's frame, but have its actual position be 10, 10.

I haven't fully fleshed this out, but you'd probably need to create a UIView subclass that had two extra bits - firstly, a 'padding' property (probably a UIEdgeInset), and then override the setFrame setter to automatically add the correct amount of padding to a frame. You could, say, have an initWithFrame:padding: method to create objects. You could even have your UIView subclass automatically query the superview to obtain the padding.

There's a lot of scope here - so I guess to answer your question, no - there isn't anything off the shelf - but implementing it yourself is pretty achievable and probably quite fun as well!

(assuming you find doing this sort of thing fun, of course...)

like image 184
lxt Avatar answered Sep 09 '25 00:09

lxt