Hi I am beginner to iOS and I have a question.
I want to re arrange multiple buttons when changing orientations so i have this code to create them :
NSArray *buttonImages = [[NSArray alloc] initWithObjects:#"button_1.png",#"button_2.png",
#"button_3.png", #"button_4.png", nil];
int xcord = 0;
//int ycord = 0;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5;
for (int i=0; i<4; i++) {
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2;
btn = [[DashButton alloc] initWithFrame:CGRectMake(buttonStartingOffset + xcord, 40, buttonWidth,buttonHeight) andImage:[buttonImages objectAtIndex:i] andTitle:#"Test"];
btn.tag = i+1;
[springboardScrollView addSubview:btn];
//[btn release];
xcord = xcord + screenHorizontalDivisions;
}
and the code to re arrange them is:
- (void)animate: (id)sender {
UIButton *button = (UIButton *)sender;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int xcord = 0;
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5; //find the divisions
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2; //center the button
NSLog (#"%i", button.tag);
switch (button.tag) {
case 1:
button.frame = CGRectMake(buttonStartingOffset, 40, buttonWidth,buttonHeight);
break;
case 2:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions, 40, buttonWidth,buttonHeight);
break;
case 3:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 2, 40, buttonWidth,buttonHeight);
break;
case 4:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 3, 40, buttonWidth,buttonHeight);
break;
}
then I am calling it when orientation changes:
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation duration: (NSTimeInterval) duration {
[self animate:btn];
How can I refer to each button one by one?
The code in animate: always return button.tag=4.
Please assist!!
Thanks
Bill.
You need to get the array of UIButtons with the following code:
NSArray* buttons = [springboardScrollView subviews]
This will return all the subviews in the scrollView. If you have UiButtons only that will be all buttons in the scroll view. If you have other types of views in scroll view then you need to iterate through array and check if the object is UiButton, which is explained on the following link .
Then you can change the frame, image, position....
Related
In my application i want to make UIButton slider with scroll view in which when we scroll uiscrollview of buttons then the button will be locate at the center please see these application's first screen sot http://itunes.apple.com/au/app/id422249255?mt=8 how can i do it and what methods i can used for it currently i am used these delegate methods of UIScrollview
-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
}
(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
}
(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView
{
}
Do this:
Add these in header of .m file
#define viewWidth 40 //button width
#define viewHeight 30 //button height
#define viewOffsetX 5 //button left ie X
#define viewOffsetY 5 //button right ie Y
#define viewXspace 15 //button from top X space
#define ViewYspace 5 //button from bottom
You have images of button add them in array ie arrBtnImages
Now use these method to load button in your scrollView ie scView in viewDidLoad method
- (void)loadBtnInSlider
{
int row = 1;
int col = [arrBtnImages count] / row;
if ([mut_arrImages count] % col != 0 ) {
col++;
}
int index = 0;
for (int i=0; i < row ; i++)
{
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.y = (i * viewHeight) + (i * ViewYspace) + viewOffsetY;
for (int j= 0; j < col && index < [mut_arrImages count]; j++) {
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.x = (j * viewWidth) + (j * viewXspace) + viewOffsetX;
frame.origin.y = viewOffsetY;
UIButton *btn = [[UIButton alloc]initWithFrame:frame];
[btn setTag:j];
[btn addTarget:self action:#selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:[arrBtnImages objectAtIndex:j]] forState:UIControlStateNormal];
index++;
[scView addSubview:btn];
[btn release];
}
}
[scView setContentSize:CGSizeMake(col * (viewWidth+ViewYspace)+viewOffsetY,scView.frame.size.height)];
}
Use this method in vieDidLoad like this:
[self loadBtnInSlider];
Now add below selector method for button in .h file and it would be like this
-(void)btnSelector:(id)sender
{
UIButton *btnSelected = sender;
switch(btnSelected.tag)
{
// add case as much u have button
case 0:
//first button called
break;
case 1:
//second button called
break;
::
::
}
}
I have used this custom grid view in my code, and I want to create a grid view which looks like this:
How should I do this in the code? Here's the setup method for items:
- (void)setupItemViews {
for (UIView *view in self.itemViews) {
[view removeFromSuperview];
}
for (UIImageView *image in self.images) {
[image removeFromSuperview];
}
[self.images removeAllObjects];
[self.itemViews removeAllObjects];
// now add the new objects
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
for (NSUInteger i = 0; i < numItems; i++) {
GridMenuItemView *itemView = [[GridMenuItemView alloc] init];
GridMenuItem *menuItem = [self.menuDelegate menuView:self itemForIndex:i];
itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
itemView.label.text = menuItem.title;
//itemView.imageView.image = menuItem.icon;
itemView.tag = i;
[itemView addTarget:self
action:#selector(itemPressed:)
forControlEvents:UIControlEventTouchUpInside];
[itemView setBackgroundImage:[UIImage imageNamed:menuItem.normalImageName]
forState:UIControlStateNormal];
[itemView setBackgroundImage:[UIImage imageNamed:menuItem.highlightedImageName]
forState:UIControlStateHighlighted];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
[self.itemViews addObject:itemView];
[self addSubview:itemView];
}
Thanks to ctrahey, I got what I wanted. But the method used for setting up the cells is -(void) layoutSubviews as follows:
You can find the corresponding code after this line: **//__n__ ...
- (void)layoutSubviews {
[super layoutSubviews];
//[self removeFromSuperview];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
[self setupItemViews];
}
CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;
CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;
// get an even y padding if less than the max number of rows
if (totalHeight < self.bounds.size.height) {
CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
yPadding += extraYPadding;
totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}
// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}
for (int i = 0, j = numColumns; i < numItems; i++) {
j--;
if (j<0) {
j += numColumns;
}
//NSUInteger column = j ;
//NSUInteger row = i / numColumns;
UIView *item = [self.itemViews objectAtIndex:i];
//CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
//CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;
**//__n__ These three lines of code make set the cell items at different xPositions from right and left.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**
//item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);
// if ((i%numColumns) == 0) {
// UIImageView *img = [self.images objectAtIndex:i/numColumns];
// img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);
// }//End If
}
//Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}
else if (UIDeviceOrientationIsLandscape(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}
}
If I understand your intention correctly, the line:
itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
Should be using your index i and some simple arithmetic to position the views. One technique I have used in the past is a simple is-odd check using the modulus operator.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
itemView.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);
just use the uitableview operation.
in that cellforrowatindexpath
set button image and frame based on the odd and even
if(indexpath.orw%2==0)
{
//set your frame here;
}
else
{
// set your frame here;
}
Thanks to ctrahey. The method used for setting up the cells is -(void) layoutSubviews as follows:
You can find the corresponding code after this line: **//__n__ ...
- (void)layoutSubviews {
[super layoutSubviews];
//[self removeFromSuperview];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
[self setupItemViews];
}
CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;
CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;
// get an even y padding if less than the max number of rows
if (totalHeight < self.bounds.size.height) {
CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
yPadding += extraYPadding;
totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}
// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}
for (int i = 0, j = numColumns; i < numItems; i++) {
j--;
if (j<0) {
j += numColumns;
}
//NSUInteger column = j ;
//NSUInteger row = i / numColumns;
UIView *item = [self.itemViews objectAtIndex:i];
//CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
//CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;
**//__n__ These three lines of code make set the cell items at different xPositions from right and left.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**
//item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);
// if ((i%numColumns) == 0) {
// UIImageView *img = [self.images objectAtIndex:i/numColumns];
// img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);
// }//End If
}
//Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}
else if (UIDeviceOrientationIsLandscape(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}
}
I'm simply trying to add a UIView for every object in an array without displaying more than 3 on screen but the views aren't next to each other. There is a big gap (one view width) between each view. Here's what I've got;
int numberOfUsersOnScreen;
if (array.count < 3) {
numberOfViewsOnScreen = array.count;
}else{
numberOfUsersOnScreen = 3;
}
double width = (self.scrollView.frame.size.width/numberOfViewsOnScreen);
CGRect r = CGRectMake(0, 0, width, 1200);
[self.usersScrollView setContentSize:CGSizeMake(width*array.count, 0)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
Try this:
int xPosition = 0;
for (int i = 0; i < users.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, 0, width, 1200)];
[self.scrollView addSubview:view];
xPosition += width;
}
Instead of
[self.scrollView setContentSize:CGSizeMake(width*array.count, 0)];
It should be
[self.scrollView setContentSize:CGSizeMake(width*array.count, self.scrollView.frame.size.height)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
In this way you have also a memory leak on view(s) object
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
[view release];
}
I'm resizing a UIView and a UIScrollView based on the number of pixels that UITextViews are being moved. I have all of the pixels being stored in int variables. At the end of my method to populate all of these text views I'm using this:
viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
realDetailsView.frame = CGRectMake(0, 20, 320, viewHeight);
viewHeight is the constant view height which is 1475. I'm using viewPixelsToBeAdded and viewPixelsToBeRemoved to hold the total number of pixels that the view needs to be resized. When I use the above code... my scrollView (detailsView) just stops scrolling. Altogether. I have my objects inside of a view. That view is inside of a scrollView. Is there an easier way to go about resizing this stuff? Why does my scrollView stop scrolling when I increase or decrease its height? Any help would be greatly appreciated. I can post the entire populateLabels method if need be. I kinda feel like I'm taking the long route with it anyway.
-(void) populateLabels {
NSString *noInfo = (#"No Information Available");
lblCgName.text = _Campground.campground;
NSString *cgLoc = _Campground.street1;
NSString *cgCity = _Campground.city;
NSString *cgState = _Campground.state1;
NSString *cgCountry = _Campground.country;
NSString *cgZipPostal = _Campground.zipPostal;
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgCity];
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgState];
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgCountry];
cgLoc = [[cgLoc stringByAppendingString:#" "] stringByAppendingString:cgZipPostal];
lblCgLoc.text = cgLoc;
double dRate = [_Campground.regRate1 doubleValue];
double dRate2 = [_Campground.regRate2 doubleValue];
NSString *rate = [[NSString alloc] initWithFormat:#"$%0.2f",dRate];
NSString *rate2 = [[NSString alloc] initWithFormat:#"$%0.2f",dRate2];
if ([rate2 isEqualToString:#"$0.00"]) {
lblRate.text = rate;
} else {
rate = [[rate stringByAppendingString:#" - "] stringByAppendingString:rate2];
lblRate.text = rate;
}
double dPaRate = [_Campground.paRate1 doubleValue];
double dPaRate2 = [_Campground.paRate2 doubleValue];
NSString *paRate = [[NSString alloc] initWithFormat:#"$%0.2f",dPaRate];
NSString *paRate2 = [[NSString alloc] initWithFormat:#"$%0.2f",dPaRate2];
if ([paRate2 isEqualToString:#"$0.00"]) {
lblPaRate.text = paRate;
} else {
paRate = [[paRate stringByAppendingString:#" - "] stringByAppendingString:paRate2];
lblPaRate.text = paRate;
}
lblLocal1.text = _Campground.localPhone1;
lblLocal2.text = _Campground.localPhone2;
lblTollFree.text = _Campground.tollFree;
lblFax.text = _Campground.fax;
lblEmail.text = _Campground.email;
lblWebsite.text = _Campground.website;
NSString *gps = _Campground.latitude;
NSString *longitude = _Campground.longitude;
gps = [[gps stringByAppendingString:#", "] stringByAppendingString:longitude];
lblGps.text = gps;
int viewHeight = 1475;
int textViewDefaultHeight = 128;
int newTextViewHeight = 0;
int highlightsPixelsRemoved = 0;
int highlightsPixelsAdded = 0;
int notesPixelsRemoved = 0;
int notesPixelsAdded = 0;
int directionsPixelsRemoved = 0;
int directionsPixelsAdded = 0;
int rentalsPixelsRemoved = 0;
int rentalsPixelsAdded = 0;
int viewPixelsToBeRemoved = 0;
int viewPixelsToBeAdded = 0;
//----------------------------------------------------------------------------
txtHighlights.text = _Campground.highlights;
[self fitFrameToContent:txtHighlights];
newTextViewHeight = txtHighlights.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
highlightsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += highlightsPixelsAdded;
txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, highlightsPixelsAdded);
txtTents.frame = CGRectOffset(txtTents.frame, 0, highlightsPixelsAdded);
txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, highlightsPixelsAdded);
txtNotes.frame = CGRectOffset(txtNotes.frame, 0, highlightsPixelsAdded);
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, highlightsPixelsAdded);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, highlightsPixelsAdded);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, highlightsPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, highlightsPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
highlightsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += highlightsPixelsRemoved;
txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, -highlightsPixelsRemoved);
txtTents.frame = CGRectOffset(txtTents.frame, 0, -highlightsPixelsRemoved);
txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, -highlightsPixelsRemoved);
txtNotes.frame = CGRectOffset(txtNotes.frame, 0, -highlightsPixelsRemoved);
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -highlightsPixelsRemoved);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -highlightsPixelsRemoved);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -highlightsPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -highlightsPixelsRemoved);
}
//----------------------------------------------------------------------------
txtTents.text = _Campground.tents;
//----------------------------------------------------------------------------
txtNotes.text = _Campground.notes;
[self fitFrameToContent:txtNotes];
newTextViewHeight = txtNotes.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
notesPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += notesPixelsAdded;
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, notesPixelsAdded);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, notesPixelsAdded);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, notesPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, notesPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
notesPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += notesPixelsRemoved;
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -notesPixelsRemoved);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -notesPixelsRemoved);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -notesPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -notesPixelsRemoved);
}
//----------------------------------------------------------------------------
txtDirections.text = _Campground.directions;
[self fitFrameToContent:txtDirections];
newTextViewHeight = txtDirections.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
directionsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += directionsPixelsAdded;
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, directionsPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, directionsPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
directionsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += directionsPixelsRemoved;
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -directionsPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -directionsPixelsRemoved);
}
//----------------------------------------------------------------------------
txtRentals.text = _Campground.rentals;
[self fitFrameToContent:txtRentals];
newTextViewHeight = txtRentals.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
rentalsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += rentalsPixelsAdded;
} else if (newTextViewHeight < textViewDefaultHeight) {
rentalsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += rentalsPixelsRemoved;
}
viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
scrollView.frame = CGRectMake(0, 20, 320, viewHeight);
}
It sounds like you're setting the same frame, which is much larger than the screen, on both the UIView and the UIScrollView? So the scroll view doesn't scroll because it's the same size as the view within it — there's nothing extra to show by scrolling.
Probably you want to leave the scroll view frame alone, so that the scroll view takes up the same amount of space on screen regardless of the size of its contents, and set the contentSize property. Similarly I'd expect you to want to pass 0 as the y coordinate for the view within the scrollview since view frames are relative to their parent.
A scrollView has two sizes: Its frame, which determines how large (and where) it is on the screen, and its contentSize, which determines how far it will scroll.
If contentSize is the same or smaller than frame.size, the scrollView will not scroll (because there is nothing to scroll to).
Usually, you'll want to set the contentSize of a scrollView to the size of the view(s) that you put into it.
Say you have a UIImageView that has a size of 1000x1000 points. Its frame will probably by CGRectMake(0, 0, 1000, 1000). Say your ScrollView is a fullscreen on an iPhone in portrait orientation, so it has a frame of CGRectMake(0, 0, 320, 480). If you now set its contentSize to CGSizeMake(1000, 1000), it will be able to scroll over all of the picture. If you make the contentSize a rectangle of 320 x 480, it won't scroll at all. And if you set the contentSize to 2000x2000, you will be able to scroll further than the picture.
How to create Grid of Buttons on iphone.
10/10 matrix...
I found NSMatrix for MAC ... Not for iphone...
any alternative way to create grid of button on my view.
#Thanks in advance.
Manually?
int rows = 10;
int cols = 10;
float gridWidth = 320.0;
float gridHeight = 320.0;
float buttonWidth = 28.0;
float buttonHeight = 28.0;
float gapHorizontal = (gridWidth - (buttonWidth * rows)) / (rows + 1);
float gapVertical = (gridHeight - (buttonHeight * cols)) / (cols + 1);
float offsetX;
float offsetY;
int count = 0;
do {
offsetX = gapHorizontal + ((count % rows) * (buttonWidth + gapHorizontal));
offsetY = gapVertical + ((count / rows) * (buttonHeight + gapVertical));
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(offsetX, offsetY, buttonWidth, buttonHeight)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
[aView release];
offsetX+= buttonWidth + gapHorizontal;
count++;
} while(count < rows * cols);
Also check out AQGridView. I guess it's pretty much what you are looking for. And more.
As I usually use this to make grid.. Only you have to just replace with your row and column
#define MatrixRow 3
#define MatrixColumn 3
#define GRID_WIDTH 300
#define GRID_HEIGHT 300
UIView *grid = [[UIView alloc] initWithFrame:CGRectMake(10, 50, GRID_WIDTH, GRID_HEIGHT)];
grid.backgroundColor = [UIColor brownColor];
for (int i = 0; i< (MatrixColumn*MatrixRow); i++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor yellowColor];
CGFloat xOrigin = GRID_WIDTH/(MatrixColumn*2) + (GRID_WIDTH/MatrixColumn)*(i%MatrixColumn);
CGFloat yOrigin = GRID_HEIGHT/(MatrixRow*2) + (GRID_HEIGHT/MatrixRow) *(i/MatrixColumn);
NSLog(#" (\"%.2f\" , \"%.2f\")",xOrigin,yOrigin);
view.center = CGPointMake(xOrigin, yOrigin);
[grid addSubview:view];
}
[self.view addSubview:grid];