Draw UIImageView at multiple places in superview - iPhone OS - iphone

I'm creating a custom progress bar and I've spliced it up into 1px images. If the progress bar were 100px wide then I would need 100 UIImageViews to fill the progress bar. The problem is this very quickly slows the iPhone down. How can I reuse an image view?
Anthony

Make your own UIView subclass and then draw the images onto it yourself in the drawRect message. Look at this
How to draw an UIImage or directly in -drawRect:?

Create The imageView in Xib (Not in View)
Create it's IBOutlet (Suppose Named MyImage)
And Try
For(int i=0;i<100;i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.frame=CGRectMake(i,100,1,1);
img.image=MyImage.image;
[self.View addSubView:img];
}

Related

add subview to UIImageView via interface builder

Is such thing possible? I have my UIScrollView created via IB and I wanted it as a subview of a UIImgeView I have, created in IB as well. If it is possible how do I do so?
Short answer NO. The workarounds depend on what's more important to you
1. Creating the UIImageView subview hierarchy in the xib is most important
Start with a UIView but change it's class to UIImageView
then set the image in code
self.myImageView.image = [UIImage imageNamed:#"someImage"];
2. Setting the image in the xib is most important
Then do as you have been doing and add the subviews in code
[self.myImageView addSubview:someSubView];
3. You want the imageView and the other view to move and scale in unison
Then place them in a container UIView that is the same size as the UIImageView
Here you can see a container UIView that holds the UIImageView and another subview
If you can't drag it into the UIImageView then I do not think it is possible,and an imageViews purpose is not meant to hold subviews. But it is just as easy to add it in code
[imageView addSubview:subView];

Setting a jpg as the background for a UIView?

I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when the iphone is rotated.
-thanks
You need to use a UIImageView, which is a subclass of UIView. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.

How to scroll the content without scrolling the image in the background using UIScrollView?

I am trying to scroll the content without scrolling the image in the background using UIScrollView.
I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.
I have also set scrolling content size in the vierController that owns this UIView
(void)viewDidLoad {
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 960);
}
My problem is that the Image only appears moves along with the content.
I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.
I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).
Any advice would be appreciated.
The easiest (and correct way) is to set an background image to your UIScrollView
UIImage *img = [UIImage imageNamed:#"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.
Thanks. This was extremely helpful.
The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).
I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:
// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);
// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:#"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".
I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.
Jason

How would I draw a line at the bottom of a UINavigationBar?

The design I've been given for an iPhone app I'm working on calls for a 1px red line at the bottom of the navigation bar. I know how to draw a line with Quartz and -drawRect, but how do I get access to the UINavigationBar's view? Or would it work to draw the line in the app's main view, on top of everything else?
Duncan Wilcox's answer below works to get the line drawn, but then buttons on the bar become impossible to press.
If I do [self.navigationItem.titleView.superview sendSubviewToBack:titleView] then buttons that are created in nib files work, but not ones that are dynamically added.
I guess the answer depends on where you want the red line, as part of the navigation bar or just under it. That would be only 1 pixel difference, but designers are demanding these days :)
If you want it below the navigation bar you definitely have to put it in all of the app's views where the line is needed.
There's a surprisingly simple way to alter the look of the navigation bar, I used it for skinning the entire navbar look but it will probably work for even only some parts of the navbar if you make the image partially transparent.
Basically we want to skin the UINavigationItem of a UIViewController that's in your navigation hierarchy.
create a 320x44 image containing your custom navbar look
add a UIImageView to the XIB where the UINavigationItem is
link the titleView property of the UINavigationItem to the UIImageView
This almost works, except that the title view will be resized in wierd ways. To fix that I have a UIImageView subclass that keeps the image properly set:
#implementation TitleViewHack
- (void)setFrame:(CGRect)frame
{
frame.size.width = 320;
if(frame.origin.x > 0 && frame.origin.x < 20)
frame.origin.x = 0;
[super setFrame:frame];
}
#end
So instead of using a UIImageView above you'll use this TitleViewHack class.
This is what I've settled upon:
// UINavigationBar subclass
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"background_navbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

transparent subview over UIimageView

as i am a new in iphone world ,i need some startup help to achieve this kind of design? background of view which has button should be transparent so that user can see product image in back.
http://www.freeimagehosting.net/uploads/cb58b72480.jpg
let me know if it requires more explanation?
thanks.
You will have to have a single UIView containing your UIImageView and UIButton.
Using InterfaceBuilder first drag the UIImageView and then drag the UIButton inside the parent UIVIew.
Or programmatically you can call the parent UIView's addSubView method first for the UIImageView and then for the UIButton view
[view addSubview:myImageView]
[view addSubview:myButton]
Either of this approach will place the instance UIButton on top of the UIImageView when the UIView is rendered
Also, FYI, transparency of a View can be controlled through its alpha property
alpha = 0 being the most transparent and
alpha = 1 being the most opaque
Hope this helps.