UIView background Color - iphone

I am trying to create a grid view by using a UITableView (my older questions have pointed me in this direction) and am currently setting up the views for the individual items. Having created a custom UITableViewCell that displays 3 items per row, I have decided to pull these items into a subclass of UIView called ItemView.
This ItemView will then be added as a subview to the custom UITableViewCell to display the grid. Anyway, I have managed to create the view and can get it to display a UILabel fine, however I am having trouble in changing ItemView to be transparent apart from the UIViews (Labels, buttons, images etc) within it. Here is my code for the UIView:
#import <UIKit/UIKit.h>
#interface ItemView : UIView {
}
#end
#import "ItemView.h"
#implementation ItemView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self setBackgroundColor:[UIColor lightGrayColor]];
}
- (void)dealloc {
[super dealloc];
}
#end
Where should I set the background color for it to work properly?
Cheers

Try:
self.backgroundColor = [UIColor clearColor];
I don't see any reason not to have that in your init method. I don't think you want to override drawRect: in this case as you'll need to draw the entire view yourself.

Related

How to add a UIImage with UIGesture to most views in an application.

I want to make an Advertising banner in my app. A bit like iAd's.
I was going to make it by having a UIImage on the view then assigning the banner image. I would then add a touch gesture so the user could click it and go to another view in my app. I Know That I can do this on one view quite easily but I want this to be on most views in the app. Whats the best way for adding the banner to more than one view with out writing the same code more that once?
The below design shows the sort of banner im after.
Thanks
#import <UIKit/UIKit.h>
#class custom;
#protocol adDelegate
- (void)viewAd:(NSString *)adRate;
#end
#interface custom : UIView
#property (strong, nonatomic) UIImage *viewImage;
#property (assign) id <adDelegate> delegate;
#end
// Main class
#import "custom.h"
#implementation custom
#synthesize viewImage;
#synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = viewImage;
[self addSubview:imageView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate viewAd:#"view"];
}
You can Create a UIView Class and call it BannerView for instance.
// in the bannerView.h
#import <UIKit/UIKit.h>
#interface BannerView : UIView{
UIImageView* bannerImage;
}
#property(nonatomic,retain) UIImageView* bannerImage;
#end
//in the bannerView.m
#import "BannerView.h"
#implementation BannerView
#synthesize bannerImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
bannerImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"banner-image.png"]];
bannerImage.frame=CGRectMake(0, 0, 320, 100);
[self addSubview:bannerImage];
// add a uibutton on top of the uiimageview and assign an action for it
// better than creating an action recogniser
UIButton* actionButton=[UIButton buttonWithType:UIButtonTypeCustom];
actionButton.frame=CGRectMake(0, 0, 320, 100);
[actionButton addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:actionButton];
}
-(void) yourAction{
// do what ever here like going to an other uiviewController as you mentionned
}
#end
Now you can call this view from any View Controller this way
BannerView* banner=[[BannerView alloc] initWithFrame:CGRectMake(0, 300, 320, 100)];
[self.view addSubview:banner];
Try creating a parent class from UIView where you do all the display and handling of the banner using your UIImageView and gesture recognizers. Then whichever views need this functionality, derive them from this parent class, and override default handling in method so that you can customize the behavior in your child class.
A few suggestions:
First, why not just use a UIButton instead of a UIImage with a Gesture? All you're really doing is replicating button functionality after all...
Second, I'd probably tackle the overall problem by creating a class that includes the UIButton, like so:
#interface YourSuperViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIButton *adButton;
- (IBAction)adTouched:(id)sender;
#end
In the viewDidLoad for this class, create the button, and add it to the view, and add your ad-specific logic to the adTouched action.
Then create the rest of the views in your app as an instance of YourSuperViewController. Like so:
#interface SomeOtherViewController : YourSuperViewController
Now the SomeOtherViewController will auto-magically have the ad button and respond to a touch on it properly. Done!
What everyone else has said is the best way. If you need custom functionality, subclassing is probably the way to go.
I just wanted to add one pedantic thing. Its important to remember that a UIImage is not a view. There has never been a UIImage on the screen, ever. A UIImage is a model object. It is just a collection of data. A UIImageView is a view object and as such, a UIImageView can display itself on the screen.
This might seem overly pedantic and nitpicky, but its important to have these things sorted out in our heads in order to effectively use MVC (model, view, controller)

Change size of UIViewTable to accommodate for AdWhirl Ad

I am trying to change the size of my UITableView. I have an ad on the bottom of my view, and when I scroll, the ad scrolls along with it. I was wondering how I can change the size of the UITableView so the ad will always remain on the bottom of the view regardless of whether the UITableView is being scrolled or not. I have tried changing the size of the frame of the TableView, but this doesn't work.
- (void)viewDidAppear:(BOOL)animated
{
tableView.frame = CGRectMake()...
}
I also tried changing it in the scrollViewDidScroll: selector, but no luck. Is there anyway I can change the height so it doesn't conflict with my ad on the bottom? Thanks!
With UITableViewControllers self.view == self.tableView. This is a problem in your case because the desired effect you want requires sibling views (two views added to a common superview) but there is no "superview" for self.tableView.
You have to create a new UIViewController subclass that has a UITableView and your ad view as two subviews. You will need to handle things like setting the data source and delegate for the table view, as well as deselecting table view cells when the controller appears. This is a little more work and requires some care, but is definitely doable.
I've thrown together a quick example below that will get you started:
// Header
#interface CustomTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (id)initWithStyle:(UITableViewStyle)tableViewStyle;
#property (nonatomic, readwrite, retain) UITableView* tableView;
#end
// Source
#interface CustomTableViewController()
#property (nonatomic, readwrite, assign) UITableViewStyle tableViewStyle;
#end
#implementation CustomTableViewController
#synthesize tableView;
#synthesize tableViewStyle = _tableViewStyle;
- (id)initWithStyle:(UITableViewStyle)tableViewStyle {
if ((self = [super initWithNibName:nil bundle:nil])) {
_tableViewStyle = tableViewStyle;
}
return self;
}
- (void)loadView {
[super loadView];
self.tableView = [[UITableView alloc] initWithStyle:self.tableViewStyle];
self.tableView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
| UIViewAutoresizingMaskFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// Create your ad view.
...
adView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
| UIViewAutoresizingMaskFlexibleTopMargin);
[self.view addSubview:adView];
[adView sizeToFit];
self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - adView.frame.size.height);
adView.frame = CGRectMake(0, self.view.bounds.size.height - adView.frame.size.height, self.view.bounds.size.width, adView.frame.size.height);
[self.tableView reloadData];
}
- (void)viewDidUnload {
self.tableView = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath* selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (nil != selectedIndexPath) {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:animated];
}
}
#end
Simple way to solve this problem is just use .XIB file for your UITableView and then you change the height very easily using Interface Builder.
If you dont have IB file then please go through this post: How do I resize the UITableView's height dynamically?

How to change the frame of UIKeyBoard programmatically in iOS

Well, i have gone through some decent goggling before posting this question but was unsuccessful in finding the right answers.
I cant really explain my entire app scenario here as it is a bit complex to explain. So, let me make this question very very simple. How can i change the frame of the UIKeyBoard.i.e. I want the UIKeyBoard to rotate or translate 90 degrees upwards to support my view position.
Is there a way out for me?
You can't change the default keyboard. You can, however, create a custom UIView to be used as keyboard replacement by setting it as inputView on, for example, a UITextField.
While creating a custom keyboard takes a bit of time, it works well with older iOS versions (inputView on the UITextField is available in iOS 3.2 and later) and supports physical keyboards (the keyboard is hidden automatically if one is connected).
Here's some sample code to create a vertical keyboard:
Interface:
#import <UIKit/UIKit.h>
#interface CustomKeyboardView : UIView
#property (nonatomic, strong) UIView *innerInputView;
#property (nonatomic, strong) UIView *underlayingView;
- (id)initForUnderlayingView:(UIView*)underlayingView;
#end
Implementation:
#import "CustomKeyboardView.h"
#implementation CustomKeyboardView
#synthesize innerInputView=_innerInputView;
#synthesize underlayingView=_underlayingView;
- (id)initForUnderlayingView:(UIView*)underlayingView
{
// Init a CustomKeyboardView with the size of the underlying view
// You might want to set an autoresizingMask on the innerInputView.
self = [super initWithFrame:underlayingView.bounds];
if (self)
{
self.underlayingView = underlayingView;
// Create the UIView that will contain the actual keyboard
self.innerInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, underlayingView.bounds.size.height)];
// You would need to add your custom buttons to this view; for this example, it's just red
self.innerInputView.backgroundColor = [UIColor redColor];
[self addSubview:self.innerInputView];
}
return self;
}
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// A hitTest is executed whenever the user touches this UIView or any of its subviews.
id hitTest = [super hitTest:point withEvent:event];
// Since we want to ignore any clicks on the "transparent" part (this view), we execute another hitTest on the underlying view.
if (hitTest == self)
{
return [self.underlayingView hitTest:point withEvent:nil];
}
return hitTest;
}
#end
Using the custom keyboard in some UIViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
CustomKeyboardView *customKeyboard = [[CustomKeyboardView alloc] initForUnderlayingView:self.view];
textField.inputView = customKeyboard;
}

Adding an overlay to a grid tableView

I have a tableview which each row has 4 images. I have implemented a share option which will allow the user to select multiple images. How can I add an overlay or some kind of visual effect to show that the image is selected?
I would like to add some overlay to display that an image is selected, but How would this be done without adding a new set of subview for each thumbnail? And once that is done, how would the selection of the new views be linked back to the images behind them so that they can be added to an array?
Or is there an easier way to do this?
Thanks
Depending on how you're implementing this grid view, it might make sense to track all of the selecting and deselecting at that level.
As for the overlay, the quick and dirty way is to subclass UIImageView, add a BOOL property called selected. Then you can override the setter for selected and handle showing or hiding your overlay view.
Here's how I would setup my subclass. First the interface:
#interface SelectableImageView : UIImageView
#property (nonatomic, assign, getter = isSelected) BOOL selected;
#end
and the implementation...
#interface SelectableImageView ()
#property (nonatomic, retain) UIView *overlayView;
#end
#implementation SelectableImageView
#synthesize selected;
#synthesize overlayView;
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
overlayView = [[UIView alloc] initWithFrame:frame];
overlayView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
overlayView.hidden = YES;
[self addSubview:overlayView];
}
return self;
}
- (void)setSelected:(BOOL)flag
{
selected = flag;
self.overlayView.hidden = !flag;
}
- (void)dealloc
{
[overlayView release], self.overlayView = nil;
[super dealloc];
}
#end

UIView and UIViewController practice for inheritance

I was just wondering if this approach looks like a good practice for apps with a lot of custom views, for nested PNG graphics and animations that may change based on user interaction. I created a BaseView class that extends UIView
#interface BaseView : UIView {
#protected
BaseViewController *controller;
}
#property (retain) BaseViewController *controller;
#end
and a corresponding controller class which is the primary location which I am putting code to manipulate the view
#interface BaseViewController : UIViewController {
#protected
CGRect drawArea;
}
- (void) drawArea:(CGRect) _drawArea;
- (CGRect) drawArea;
- (void) linkSubviewController: (BaseViewController *) _subviewController;
#end
where "drawArea" is the CGRect used to pass to the view as a frame.
"linkSubviewController" allows you to nest a controller and view as follows :
- (void) linkSubviewController: (BaseViewController *) _subviewController {
[self.view addSubview:[_subviewController view]];
}
In addition I layered another custom pair called "ImageView" and "ImageViewController" which extend BaseView but also store a UIImage and an x,y,w,h
In the "drawRect" drawing methods on views I can check to see if any vars in the self.controller vars have been changed, or assign images, for example :
UIImage *image = [(ImageViewController *)self.controller image];
CGContextDrawImage( ... ) etc
I write most of the loadView methods something like this
- (void)loadView {
ImageView *v = [[ImageView new] initWithFrame:drawArea];
v.controller = self;
self.view = v;
}
The base "initWithFrame" routine contains
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
So I can load a variety of images with transparent backgrounds without having to assign that each time.
I've been able to use this code throughout my app and it seems to make it easy to write a top level class which assembles the layout of custom items. For animations I've been putting them in the controllers and manipulating self.view.layer.
Basically I am looking for feedback, I am new with Objective-C and the IPhone SDK
There are several issues here:
Using [[Classname new] init...] is incorrect usage of new. Using new is short for [[Classname alloc] init] so you are effectively calling init twice.
Views shouldn't really need to know who is controlling them.
Your view is retaining the controller, and since UIViewController retains its view, you have a retain cycle and neither will ever be fully released.
If you want this type of behavior (whereby a view can delegate its drawing to a parent), try creating a DrawDelegate protocol, have your controllers implement that protocol, and in your view subclass have a non-retaining drawDelegate property:
#protocol DrawDelegate
- (void) drawArea:(CGRect)rect;
#end
#interface BaseView : UIView {
id<DrawDelegate> drawDelegate;
}
#property (assign) id<DrawDelegate> drawDelegate;
#end