UIView not being positioned with setFrame - iphone

I'm probably not doing this correctly. Having not done an iPad app before I'm doing what seems natural to me.
I have a UIView that is going to be made of up other UIViews. Specifically, I want to dynamically create and add subviews to each of these subviews. In other words:
UIView
|
| --> UIView
|
| --> Dynamic UIView 1
| --> Dynamic UIView 2
| --> ...
I managed to get 'Dynamic UIView 1' by adding to its initWithFrame this bit of code to load the xib:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];
It works! The problem I'm having is that 'Dynamic UIView n' is always rendered at the top left corner of the screen. If I try to change the position with setFrame I get a black box at representing the frame however the actual control content remains in the top left.
Any ideas? Is there a better way to do this?
----- Update -----
Here is a screenshot of the root view.
Overview of main UIView http://www.freeimagehosting.net/uploads/460090996d.png
The entire control is call CurrentItemsViewController. The green area is a UIView within that I have linked back in code via a link in the IB, in the code sample it is called "itemsUIView". Here is how I am adding items within CurrentItemsViewController.m
- (id)init {
[super initWithNibName:nil
bundle:nil];
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Current"];
/* Approach 1 */
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *subItemView1 = [nibViews objectAtIndex:0];
/* Approach 2 - this apprach does Approach 1 in the initWithFrame method */
//ItemView *subItemView1 = [[ItemView alloc] initWithFrame:CGRectMake(120, 120, 354, 229)];
/* Approach 3 */
//ItemViewController *ctr = [[ItemViewController alloc] init];
//UIView *subItemView1 = [ctr view];
[[self view] addSubview:subItemView1];
// [[self itemsUIView] addSubview:subItemView1]; <-- doesn't render subItemView1 at all
[subItemView1 release];
[subItemView1 setFrame:CGRectMake(120, 120, 354, 229)];
[subItemView1 setBounds:CGRectMake(120, 120, 354, 229)];
return self;
}
Inside of ItemView.m I have this, it only gets call by Approach 2 and 3.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];
}
return self;
}
As shown, I tried creating the ItemView 3 different ways and each one renders the same result. When I add the view as a subview of the root view I get this. Notice that a square appears where it should render, however ViewItem actually renders at the top left corner. In the sample above, when I add subItemView1 it doesn't render anything at all.
alt text http://www.freeimagehosting.net/uploads/c6635ce860.png

Try setting the frame before you add it as a subview.
Your code:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
self addSubview:infoView];
Should be:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
infoView.frame = CGRectMake( put your rect here );
[self addSubview:infoView];

Related

how to eliminate the blank frame while flipping over to the next view

i am currently using this(AFKPageFlipper)
to produce the desired flipboard animation in my application
,there are 4 static html pages named 1.html,2..so on
loadView
- (void) loadView {
[super loadView];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
flipper = [[AFKPageFlipper alloc] initWithFrame:self.view.bounds] ;
flipper.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSURLRequest *urlReq=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"1" ofType:#"html"]isDirectory:NO]];
;
//[web loadRequest:urlReq];
NSURLRequest *urlReq1=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"2" ofType:#"html"]isDirectory:NO]];
NSURLRequest *urlReq2=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"3" ofType:#"html"]isDirectory:NO]];
;
NSURLRequest *urlReq3=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"4" ofType:#"html"]isDirectory:NO]];
arr=[[NSArray alloc]initWithObjects:urlReq,urlReq1,urlReq2,urlReq3,nil];
flipper.dataSource = self;
[self.view addSubview:flipper];
}
data source for the AFKFlipper
- (NSInteger) numberOfPagesForPageFlipper:(AFKPageFlipper *)pageFlipper
{
NSLog(#"%i",arr.count);
return [arr count];
}
- (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper
{
web= [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, 320, 385)];
[web loadRequest:[arr objectAtIndex:page-1]];
return web;
}
i can flip across multiple webviews,but the problem i am facing is when i am halfway flipping the page i cant see my next view,it seems to be a blank page,it gets loaded after i flip my page completely
In the flipboard you can see the next view partially and previous view partially,what should i do to show my next view when the current view if halfway flipped
I tried AFKPageFlipper,, but i dont think messing with its original code is any solution.
what u can try is, keep ready all the views u r going to provide to AFKPageFlipper.
NSMutableArray *webViewArray;
-(void)viewDidLoad
{
webViewArray=[[NSMutableArray alloc]int];
for(int ii=0;ii<4;ii++)
{
web= [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, 320, 385)];
[web loadRequest:[arr objectAtIndex:ii]];
[webViewArray addObject:web];
web=nil;
}
}
- (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper
{
return [webViewArray objectAtIndex:page];
}

Subclassed UIView not displaying UIImageViews added to it

So I have class called CSImageViews (subclass of uiview), that is essentially a row of UIImageViews enclosed in a single subclassed UIView.
I've added a custom init method like so (node/yaml contains uiimage name data):
- (id)initWithFrame:(CGRect)frame withNode:(NSDictionary *)node
{
self = [super initWithFrame:frame];
if (self) {
_node = node;
_imageViewYAML = [node objectForKey:#"items"];
_imageViews = [self getImageViewsForItems:_imageViewYAML];
}
return self;
}
And my getImageViewsforItems is like so (adds them all to subview):
-(NSArray *)getImageViewsForItems:(NSArray *)items
{
NSMutableArray *ivs = [NSMutableArray arrayWithCapacity:[items count]];
for (int i = 0; i < [items count]; i++) {
NSDictionary *item = [items objectAtIndex:i];
NSString *type = [item objectForKey:#"type"];
NSString *name = [item objectForKey:#"name"];
UIImage *image = [UIImage imageNamed:name];
UIImageView *iv = [[UIImageView alloc] init];
iv.image = image;
iv.tag = i;
[ivs addObject:iv];
[self addSubview:iv];
}
return ivs;
}
Now when I add this custom view to my main view like this nothing happens:
CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
[view addSubview:imageViews];
But if I add this 'csimageviews' container into a new uiview first it appears:
CSImageViews *imageViews = [[CSImageViews alloc] initWithFrame:frame withNode:node];
UIView *v = [[UIView alloc] initWithFrame:frame];
[v addSubview:imageViews];
[view addSubview:v];
thoughts?
Stupid error by me. In this particular subclassed UIView I have a method that was called every time its parent view controller was shown. This method actually removed all subviews from the view... so I wasn't able to see the uiimageviews when I rendered it in the initial view... doh.

How to access the controls on UIView which is added to a UIScrollView

I have a UIViewController in storyboard that includes a scrollView.
I want to put two UIImageViews and some UILabels on it.
the idea that I am thinking is : create a .xib file. Put a UIView on it, an then set my UIImageview and labels on this UIView.
Finally add my UIView which is a .xib file to my UIScrollView like this:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"OfferView" owner:self options:nil];
UIView *newPageView = [views lastObject];
self.newPageView.contentMode = UIViewContentModeScaleAspectFill;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
Now I want to set Text and image for the controls on the UIView programatically in my UIViewController. I try this but it did not work:
OfferUIView *offer2 = [[OfferUIView alloc] init];;
offer2.labelCompany.text = #"some text";
I also defined OfferUIView in the interface of ViewController and then synchronize, but it did not help also:
#property (nonatomic, assign) OfferUIView *offerView;
Can you help me?
Set the pointer for your OfferView when you load the nib to your offerView property
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"OfferView" owner:self options:nil];
self.offerView = [views lastObject];
self.offerView.contentMode = UIViewContentModeScaleAspectFill;
self.offerView.frame = frame;
[self.scrollView addSubview:self.offerView];
[self.pageViews replaceObjectAtIndex:page withObject:self.offerView];
And when you want to change it:
self.offerView.labelCompany.text = #"some text";
OfferUiView has to "initWithFrame" as it is required method for any view of class 'UIView' or set a frame to this view.Since u have subclassed UIView include initWithFrame as its constructor Further, since u have programmatically allocated the OfferUIView u have to add this view to your currentview i.e [self.view addSubView:offer2];
You can set a unique label for the tag and you can search for the label in the parentView like this:
UILabel *theLabel = (UILabel *)[parentView viewWithTag: 10321];

custom View for universal application

I believe, that this question is duplicated, but i can't find it =(.
How to create own UIView class, which is loaded from (iPhone/iPad)*.xib
I was trying next things:
#interface CurtainView : UIView
...
- (id)init {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_iphone" owner:self options:nil] objectAtIndex:0];
[self setFrame:CGRectMake(0, 0, 320, 460)];
}
else {
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_ipad" owner:self options:nil] objectAtIndex:0];
[self setFrame:CGRectMake(0, 0, 768, 1004)];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"there should be some animation on view appirance");
}
and ...
CurtainView* curtain = [[CurtainView alloc] init];
NSLog(#"before");
[self.view addSubview:curtain];
[curtain drawRect:CGRectMake(0, 0, 320, 460)];
But in this case, I haven't the result which I'm expecting, and drawRect isn't calling. I hope there is easy way to create custom Views for universal Apps.
put this code in .h file
#import
#interface CurtainView : UIView
#end
put this code in .m file
#import "CurtainView.h"
#implementation CurtainView
- (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
NSLog(#"there should be some animation on view appirance");
}
now call this UIView form your viewcontroller class. this will call drawRect
CurtainView* curtain = [[CurtainView alloc] init];
NSLog(#"before");
[curtain drawRect:self.view.frame];
[self.view addSubview:curtain];
you can call your viewcontroller class
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
ok. sorry, I got my misstake:
it's need to use :
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"Curtain_iPhone" owner:self options:nil] objectAtIndex:0]];
instead my:
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_iphone" owner:self options:nil] objectAtIndex:0];
Hope it will helpful for some one.
moreover, I don't know why, but property "opaque" for the View should be set in "init" method, not only in IB.

Loading Scroll Views from plists

here is the deal: For a news app, I'm loading a view inside a Scroll View for horizontal paging on each section, no problem with that, the PageControl demo helped a lot, but now I need to load a View inside another Scroll View programmatically so I can show the content of each section, the deal is that I need custom designed views already on a NIB file so each section looks different, so I would like to use a plist to load each custom view on the generic Scroll View.
Here is what I got until now:
- (void)loadScrollViewWithPage:(int)page{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
Seccion *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[Seccion alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
NSDictionary *numberItem = [self.contentList objectAtIndex:page];
//UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image1.jpg" ofType:nil]];
controller.sectionTitle.text = [numberItem valueForKey:Nombre];
controller.sectionView.text = [numberItem objectForKey:Vista];
controller.sectionId.text = [numberItem valueForKey:Id];
//Here is supposed to have the code to load the view inside the scroll view I tried using: [controller.sectionScrollView addSubview:controller.sectionView.text];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
Thanks a lot in advance
If all your views have it own nib, this might work:
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0];
so you can save the name of each nib in a plist.