I want my views to have shadows. When I first added shadows to all my views I quickly encountered a problem, the UI was very laggy. I then realised that I should instead use UIBezierPath and set shouldRasterize to true when drawing shadows.
Everything became silky smooth again but with one problem, these shadows are static. They won't resize with the views.
These views are each in their own UITableViewCell and these cell's heights are dynamically adjusted to fit the content in the views. But when a certain cell resizes (with an animation) I would like the views and their shadows to resize as well but they won't because they are rasterized.
Does anyone have any ideas as to how I can fix this problem?
Will it be appropriate to make this shadow stretchable?
If so, you can try drawing your shadow once to a shared UIImage (and cache it)
UIBezierPath to UIImage
And then set background UIImageViews with resizeable UIImage to your views with the help of UIImage's method
resizableImageWithCapInsets:
You will have one image for all shadows that will be added as background UIImageView to your views.
Related
Okay... so I am getting the hang of iOS, have created a UIView with a combination of widgets (buttons, UITableViews) content and some rendering in drawRect.
The rendering is actually to draw a rounded rectangle within the UIView frame, with circular corners. Then I add this view to my main view.
In the rotation animation, I change the position and aspect ratio of my UIView and called a layout method, and was pleased that it would smoothly change its shape and reposition its contents.... EXCEPT: something odd happens to my rendering... they sort of get squished, and my rounded corners are now elliptical/squished (even though the rendering code always makes circles) like the UIView applies a transform after the drawRect, but does;t re-render.
This is a very surprising effect, can someone give me a hint to what may be going on? I want the rendering to be consistent and sensitive to the current rectangle.
EDIT: Added pictures. The UIView onDraw renders framing and headers, and there are two UITableViews as children. It starts up in portrait mode (figure 1) and looks fine. When the parent view rotates I initiate an animation that changes the sizing of the subview to be suitable to landscape. The sub UITableViews resize fine, but the rendering is now squished (Figure 2).
You need to set the contentMode of your UIView to Redraw
How do you add a simple two color gradient as background to a UICollectionView. It should cover the whole background and stay fixed even when scrolling the collection view (horizontally).
All layer based solutions I tried so far had issues regarding not covering the whole screen and covering only the initially visible frame. Bonus points for being animatable and not using images ... ;)
What's the best way to do this?
An easy and fast solution is to set the background color of the UICollectionView to 'clear' and add another UIView with the gradient behind it.
May be the question is elementary, but I have a bug with setBackgroundColor.
I have the UIView which contain UITableView. UITableView is clearColored. I'm trying to set background on UIView:
[self.viewForTable setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgIngrList.png"]]];
On normal iPad it's looks like:
But on retina iPad (like the second image began showing):
I have two images "bgIngrList.png" and "bgIngrList#2x.png" with 290x260 and 580x520.
Where can be the bug?
EDIT
I have solved the problem by using UIImageView instead of UIView.
Thanks to everybody!!!
colorWithPatternImage: (as the name suggests) thinks your image as pattern to fill. so if the image you give is smaller than the size of the view, its gonna draw the pattern image again rather than stretching it. that is what happening here. your image's size is smaller than view's size hence its drawing the image again at the bottom. You can avoid this by adding a UIImageView and setting the image property rather than setting the backgroundColor of UIView.
I know you can mask an image in ios, but can you mask a scrollview? I'm wanting to alpha mask my scrollview so that the content on the ends appears to fade out before it gets clipped by the scrollview. Is something like this possible?
FYI I have already tried work-arounds that "fake" this effect and they don't work due to a dynamic background. I need strict alpha masking on the scrollview if at all possible.
You can fake this effect by laying over an image with alpha over the end of the scrollview. That way the fading effect stays in place, and the scrollView appears to fade out.
You can do this nicely by using layer masking:
create a mask with the appropriate alpha gradient
add the mask to the table
implement the scroll delegate, to reposition the mask to the current viewport when the table gets scrolled.
It is all explained very well here: http://www.cocoanetics.com/2011/08/adding-fading-gradients-to-uitableview/
Could you implement a footer (or last cell) that has the appropriate alpha fade to give you the effect you are looking for?
I have universal app. In that app I need the background image to be centered or stretched. That way when I rotate the device or switch it the image displays correctly.
Here is my code in viewDidLoad:
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tux.png"]]
Can anyone tell me how to accomplish this?
I think you can't easily make it centered with backgroundColor. You may:
Stretch it before setting the image as backgroundColor, see How to fill background image of an UIView.
Create a customized UIView subclass and draw the background image yourself by overriding drawRect: method;
Or add a UIImageView to you view as the background. See "UIView Class Reference: Alternatives to Subclassing":
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.