I need to create a UIButton programatically and then put it on a created UIScrollView and then put the UIScrollView on a UIView. If add these elements to self.view they are displayed, but when I want to nest then they are not displayed.
Here is what I have so far:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
viewWithPictures.contentSize=CGSizeMake(160*[smallImagesFromGallery count], self.bottomView.frame.size.height);
viewWithPictures.backgroundColor=[UIColor greenColor];
NSLog(#"Number of small images: %i",[smallImagesFromGallery count]);
for(int i=0; i<[smallImagesFromGallery count]; i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
[btn setBackgroundImage:[smallImagesFromGallery objectAtIndex:i] forState:UIControlStateNormal];
if (btn==nil) {
NSLog(#"Button is nil");
}
btn.tag=i;
[btn addTarget:self action:#selector(viewLargeVersion:) forControlEvents:UIControlEventTouchUpInside];
[viewWithPictures addSubview:btn];
}
[bottomView addSubview:viewWithPictures];
When you're setting the frame of a view that will become a subview, you need to reference the bounds of the view that it will be added to. So I think you need to change a couple of lines:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
should be:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.bounds];
and
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
should be:
btn.frame=CGRectMake(i*160, 0, 150, 100);
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
to
viewWithPictures=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.bottomView.frame.size.width,self.bottomView.frame.size.height)];
and this
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
to
btn.frame=CGRectMake(i*160, self.bottomView.frame.origin.y, 150, 100);
This is just a suggestion.
Hard to tell without the rest of the code but could it just be that you aren't adding your container view (bottomView) to your view? Which you can do by adding this at the end: [self.view addSubview: bottomView]
Related
I have defined a uiscrollview and used a for loop to add buttons to it, but whenever I run the program, I come up with a blank screen.
Here is my viewDidLoad, this is all the code i have relating to this.
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
names = [[NSMutableArray alloc]initWithObjects:#"Excavations",#"Concrete",#"Framing",#"Insulation",#"Drywall",#"Finish Carpentry",#"Paint",#"Electrical",#"HVAC",#"Plumbing",#"Roofing",#"Tile",#"Doors",#"Windows",#"Flooring",#"Countertops",#"Landscape",#"Hardscape", nil];
for (int i = 0; i < 18; i++) {
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 50*i, 320, 50);
button.tag = i;
[button addTarget:self action:#selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [names objectAtIndex:i];
button.hidden= NO;
[scrollView addSubview:button];
}
// Do any additional setup after loading the view, typically from a nib.
}
You need to set up the type for button manually, or just initialize with it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Edit: you also forgot to add your scrollview to main view
[self.view addSubview:scrollView];
You have to add the UIScrollView to your view. Try
...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView];
...
In one of my app's user interfaces i want my app display a scrolling view(the count of the scrolled pages is dynamically set in runtime). But i want the to add UIWebView and UIView elements to the same array, so that when i scroll i will be able to display both UIWebView(let's say a web page) and some other View?
EDIT
I received this answer which looks as if exactly ok but doesn't work(or i couldn't to)
here is the code:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Can anyone please give a hend of help? I need to create a scrollable array of views(like the main page of the iPhone) but the first page(mean, view) must be a member of UIWebView(the others are UIView)
And below is all my code(i han't try to do anything here:)
CGRect frame;
[webView initWithFrame:frame];
NSArray *colors = [NSArray arrayWithObjects:webView, [UIColor blackColor],[UIColor blackColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[subview setTag:i];
if([subview tag]==2){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(70.0f, 600.0f, 250.0f, 60);
[button setTitle:#"Read Messages" forState:UIControlStateNormal];
for(NSDictionary* buttonData in butDat) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle = [buttonData objectForKey:#"name"];
NSString* buttonID=[buttonData objectForKey:#"order_number"];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setTag:[buttonID intValue]];
button.titleLabel.font=[UIFont systemFontOfSize:28];
CGFloat yPosition = 60.0f;
const CGFloat buttonHeight = 85.0f;
if (button.tag==1) {
button.frame = CGRectMake(70.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==2) {
button.frame = CGRectMake(440.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==3) {
button.frame = CGRectMake(70.0f, 200.0f, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==4) {
button.frame = CGRectMake(440.0f, 200.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==5) {
button.frame = CGRectMake(70.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==6) {
button.frame = CGRectMake(440.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
[button addTarget:self action:#selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
//
[button addTarget:self action:#selector(readM:) forControlEvents:UIControlEventTouchUpInside];
// [scrollView addSubview:button];
[self.view addSubview:button];
// [self.scrollView addSubview:button];
}
[self.scrollView addSubview:subview];
There's nothing stopping you from adding objects of type UIWebView and UIView to the same NSArray.
If your code needs to know if you're dealing with a UIWebView or a UIView, you can use the method isKindOfClass: to check:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Furthermore, UIWebView is a subclasses of UIView and inherits many of the same methods, making view layout the same in both cases.
From a user interface standpoint, it's probably bad practice to have scroll views inside scroll views, so you're going to want to disable scrolling of your UIWebView objects. If you are targeting iOS 5 and above you can use the following:
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
and if you're on a version of iOS lower than 5.0, you can inject the following javascript:
<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}
</script>
To implement the javascript injection, you can use UIWebView's stringByEvaluatingJavaScriptFromString: method inside webViewDidFinishLoad:
See here for more information on UIWebView javascript injection: http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
Edit: Looking at your code, it seems as thought you're sticking a UIWebView and two UIColor objects into your array. Therefore you need to check if you're dealing with a UIWebView or a UIColor, or you'll crash on subview.backgroundColor = [colors objectAtIndex:i]; Also you set your UIWebView object's frame to CGRect frame; which is uninitialized.
You should populate your NSArray with your UIWebView and then two UIView objects with their background colors set:
UIWebView *webView = [[UIWebView alloc] init];
UIView *viewOne = [[UIView alloc] init];
[viewOne setBackgroundColor:[UIColor blackColor]];
UIView *viewTwo = [[UIView alloc] init];
[viewTwo setBackgroundColor:[UIColor blackColor]];
NSArray *viewArray = [NSArray arrayWithObjects:webView, viewOne, viewTwo, nil];
for (id theObject in viewArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Yes. UIWebViews descend from UIViews. You may use them wherever you would insert a UIView as a subview, including within a UIScrollView parent view, or as an object to be inserted into an array.
Your scroll view is not scrolling because you didn't set the contentsize property. First you have to find the content size appropriately, and then you have to set content size as
[scrollView setContentSize:CGSize];
at the end of code. ie; after
[self.scrollView addSubview:subview];
For a blind try, use
[scrollView setContentSize:CGSizeMake(1000,1000)];
this will definitely scroll the view!!!
I am using a ABUnknownPersonViewController for displaying contact information within UINavigatonController and I want a button at the bottom which will push another viewController with additional information on that user. I have been able to add a button to the bottom and I can scroll down and view it, but it does not stay on screen.
I am looking for the same thing, but can you please show me how did you add a button at the bottom?
What I did is add a button at the top, inside the navigation bar, although it does have a few visual problems, this is how i did it.
//I created a UIToolBar
UIToolBar *myToolBar = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//then set the style to match my navBar
[myToolBar setBarStyle:UIBarStyleBlackOpaque];
//and finally created the button.
NSMutableArray *buttons = [NSMutableArray alloc] initWithCapacity:1]autorelease];
UIBarButtonItem *details = [[[UIBarButtonItem alloc] init]autorelease];
[details setTitle: #"details"];
//after that placed it on top of the UPVC
unknownPersonViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myToolBar];
I hope this helps you in some way...
- (void)loadView
{
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Horray Button!!" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 500.0, 160.0, 40.0);
//[self.view addSubview:button];
NSArray *subviews = [self.view subviews];
NSLog(#"subviews: %d", [subviews count]);
UIScrollView *v = [subviews objectAtIndex:0];
[v addSubview:button];
CGSize cgSize = [v contentSize];
cgSize.height = 500.0 + 40.0;
[v setContentSize:cgSize];
}
The code I am using to add the button down at the bottom, but it does not stay in view once scrolled to.
If I add the button straight everything goes fine. I'd really like to add it inside a view and still be able to click it. Here's the code:
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];
Did you set the footer height to the height of the view you are adding to it? I had a similar problem when I returned a UIView containing a UIButton in the method viewForFooterInSection. The footer height was not large enough until I updated the heightForFooterInSection method. For example:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return containerView.bounds.size.height;
}
All works fine! In your TableViewController do:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];
[footerButton setTitle:#"Touch me!" forState:UIControlStateNormal];
containerView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
self.tableView.tableFooterView = containerView;
}
- (void)click {
NSLog(#"I am here!");
}
This #selector(click) looks a little odd. I would have thought your click function would like something like:
- (void)click:(id)sender;
If that's what you have then you need to add the colon #selector(click:) to make the signatures match.
I am trying to add a UIButton at runtime however it is not visible. What am I doing wrong?
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
btn.center = self.center;
[self addSubview:btn];
}
return self;
}
First, make sure the initWithFrame: method is being called. If your view is in a Nib, initWithCoder: is being called instead.
Second, is the button the only subview (from your code it looks like it is, but you never know). The button could be hidden behind another subview. Call bringSubviewToFront: if you need to.
Finally, is the view itself visible? Is it big enough to show the button? Given your example, if the view is less than 100 pixels wide, the button won't show because it will get clipped by the view's bounds.
You must release btn and remove ":" in buttonClick:
UIButton *btn= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
if it still doesn't work, try removing the : at the end of the selector name: #selector(buttonClick)
You don't need to retain the UIButton because it is retained by [self.view addSubview:btn];
First check whether your code is executing the initwithFrame method.
Because if you are loading the view from nib i.e using
NSArray *xibviews = [[NSBundle mainBundle] loadNibNamed: #"MySubview" owner: mySubview options: NULL];
MySubview *msView = [xibviews objectAtIndex: 0];
[self.view addSubview:msView];
Then the initWithFrame part will not be executing.So please check once.