07-10-2010, 07:43 AM
#1 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Buttons on a scrollview
How do i add a button to a scrollview when there is no NIB involved?
below is the code i thought might work, the scrollview part works fine i just can't see the button. any ideas?
-(void)loadview {
self.scrollview1 = [[UIScrollView alloc] init] autorelease];
self.scrollview1.delegate = self;
self.scrollview1.pagingEnabled = YES;
self.scrollview1.showsHorizontalScrollIndicator = NO;
self.scrollview1.showsVerticalScrollIndicator = NO;
self.view = self.scrollView1;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 60, 100, 30);
button setTitle:@"Click Me!" forState:UIControlStateNormal];
[self.scrollView1 addSubview:button];
}
Thanks
Mick
07-11-2010, 02:30 AM
#2 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Anybody got any ideas?
any help would be much appriciated.
Mick
07-11-2010, 02:38 AM
#3 (permalink )
New to the SDK
Join Date: Jul 2010
Location: Portland, Oregon
Age: 19
Posts: 165
is it roundedrect or roundrect? withouth the roundED.... idk could be the problem....
Last edited by franzwarning; 07-11-2010 at 02:40 AM .
07-11-2010, 03:26 AM
#4 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Hi, thanks for the reply.
roundedrect is correct, any more ideas?
Mick
07-11-2010, 05:51 AM
#5 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Can you even see the scrollview? You don't give it a frame anywhere, which could be a problem.
07-11-2010, 07:02 AM
#6 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Hi, I can see the scrollview and it works fine.
It's just the button that seems to be a problem.
Thanks
Mick
07-11-2010, 07:14 AM
#7 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Using your code in a new project, I cannot see the scrollview or the button. Giving it a frame, I can see both the scrollview and the button.
Something to bare in mind as well is that loadView will not work if you are using a XIB.
Code:
- (void)loadView {
self.scrollview1 = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; // Dont hard code values in actual app.
self.scrollview1.delegate = self;
self.scrollview1.pagingEnabled = YES;
self.scrollview1.showsHorizontalScrollIndicator = NO;
self.scrollview1.showsVerticalScrollIndicator = NO;
self.view = self.scrollview1;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 60, 100, 30);
[button setTitle:@"Click Me!" forState:UIControlStateNormal];
[button sizeToFit];
[self.scrollview1 addSubview:button];
}
07-11-2010, 09:15 AM
#8 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Hi Harry, thanks for the reply, you are right about the frame that solved the issue. I've tested it without a NIB file and it works, this has highlighted another couple of issues.
My scrollview displays a number of images which i can swipe through. The button moves off screen when i swipe the first image also the button is behind the actual image.
How can i position the button above the image and also stop it scrolling.
Thanks for your help so far.
Mick
07-11-2010, 10:10 AM
#9 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
I would suggest adding the scrollview as a subview to the main view, rather than setting that scrollview as the main view.
Then you would add the button to the main view and not the scrollview. That way it won't scroll and will be on top of the scrollview, providing you add it last.
07-11-2010, 02:19 PM
#10 (permalink )
Registered Member
Join Date: Jun 2010
Location: Darlington UK
Posts: 84
Thanks for that Harry, it's working now.
Mick
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 325
14 members and 311 guests
akphyo , alexP , cgokey , EXOPTENDAELAX , flamingliquid , guusleijsten , ohmniac , Paul Slocum , PavelSea , SLIC , Sloshmonster , Sonuye857 , v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac