Shadow or border around scrollview iphone

I would like to draw a border / shadow around the uiscrollview, I know that I can get there with an extra view or scroll, but I don’t like handling flaws, but I heard that it should be possible to draw a border to the scrollview, and that’s what I would preferred.

I am calm for the development of iphone, any answer would be helpful.

+6
iphone border uiscrollview scrollview shadow
source share
4 answers

If you use the layer property of your scroll view (or any UIView), you can easily get a solid border ...

#import <QuartzCore/QuartzCore.h> ... myView.layer.borderWidth = 2; myView.layer.borderColor = [UIColor blackColor].CGColor; 

You can also use a layer to apply shadows in real time by setting the properties of layer.shadow* , but performance can be slow with this technique, so I usually prefer to use the following more complex, but more efficient technique. You can create a PNG image with transparency in the middle and shadow around the edge - it should have 9 different areas: 4 for each corner, 4 for each edge and a completely transparent 1x1 size in the middle. For example, if your shadow extends 6 pixels in your image, your image will be 13x13 with borders of 6 pixels / tall and 1x1 in the middle. Then you set it as a scalable image using:

 newImage = [image stretchableImageWithLeftCapWidth:6 topCapHeight:6]; 

UPDATE:. Since iOS 5.0 stretchableImageWithLeftCapWidth:topCapHeight: deprecated, use it only if you still want to support iOS 4.x devices. If you want to support only iOS 5.0+ devices, use this instead:

 newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)]; 

Then you put the image in the parent view so that it occupies the entire scroll area. If you want the shadows to go over your scrollable elements (so that your scroll view looks like an insert / behind the rest of the page), then place a transparent UIView on top of the top with a shadow image on it so that it appears in your scroll view behind it.

+17
source share

Do not forget:

 #import <QuartzCore/QuartzCore.h> 

If you want to use

 myView.layer.borderWidth = 2; myView.layer.borderColor = [UIColor blackColor].CGColor; 
+5
source share

To get CGColorRef from UIColor, you can also use this example:

 myView.layer.borderWidth = 2; myView.layer.borderColor = [UIColor blackColor].CGColor; 
+2
source share

myView.layout.borderColor generates an incompatible pointer warning when using UIColor and does not change the borders for me.

Using CGColorRef worked in my place:

 CGFloat lComponents[4] = {0,0,0,1}; CGColorSpaceRef lColorSpace = CGColorSpaceCreateDeviceRGB(); myView.layer.borderColor = CGColorCreate(lColorSpace, lComponents); CGColorSpaceRelease(lColorSpace); 
+1
source share

All Articles