I have added some images in scroll view I got prom is images not displaying on scroll view so please help, my code is below.
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
//img.tag = i;
//[button setTitle:#"Excavations" forState:UIControlStateNormal];
img.frame = CGRectMake(160.0, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}
Try this,
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
img.frame = CGRectMake(btnX, 105, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 150);
}
You set x of img frame to 160.0, so all img will be added to the same position, change it to btnX and update btnX on each iteration. Next is contentSize of scrollView, its content height is set to 150 and img is added to y of 200. So content beyond the height of scrollview cannot be visible.
You are calculating wrong frame of image view
img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);
Your scrollview's height is 150, and your buttons y value is 200, that means is out of visible frame.
-(void)addscroll
{
CGFloat btnX = 160.0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 0 ; i < numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
//img.tag = i;
//[button setTitle:#"Excavations" forState:UIControlStateNormal];
img.frame = CGRectMake(i*btnX +10, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
}
scrollView.contentSize = CGSizeMake((btnX + 10)*numberOfButton, 160);
}
Set img.frame = CGRectMake(160.0, 0, 160.0, 40.0);
By setting y = 200, you are making it out of visible parent's frame. Scroll's max height is 198, your image's y origin should be before that in order to make subview visible.
Use this code. Also make sure that img.image is not nil and "1.png" exists. You have limited the scroll to less than 200 while image start from 200 in y
-(void)addscroll
{
CGFloat btnX = 0;
int numberOfButton = 10;
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 1 ; i <= numberOfButton; i++)
{
UIImageView *img=[[UIImageView alloc]init];
img.image=[UIImage imageNamed:#"1.png"];
img.frame = CGRectMake(btnX, 200.0, 160.0, 40.0);
[scrollView addSubview:img];
btnX = btnX + 165.0;
}
scrollView.contentSize = CGSizeMake(btnX + 50, 250);}
Related
i want to display some images on album cover on my app like bellow image.
EDIT:-
i want to display this view in UICollectionViewCell and collectionView scrollDirection Horizontal with paging enabled.
any help will be appreciated. and thanks in advance.
I do not know if you understand the question correctly.
If you want to do what I see on the picture, so try this:
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
cover.backgroundColor = [UIColor blueColor];
int width = 200;
int height = 350;
int posx = 100;
int posy = 50;
for (int i = 0; i < 10; i++) {
//craete images
UIView *imagebg = [[UIView alloc] initWithFrame:CGRectMake(posx + arc4random_uniform(20), posy + arc4random_uniform(20), width, height)];
imagebg.backgroundColor = [UIColor whiteColor];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, width - 40, height - 50)];
image.image = [UIImage imageNamed:#"loading-screen.png"];
[imagebg addSubview:image];
//rotate
double rads = arc4random_uniform(100)/10;
CGAffineTransform transform = CGAffineTransformRotate(imagebg.transform, rads);
imagebg.transform = transform;
//add as subview
[cover addSubview:imagebg];
}
[self.view addSubview:cover];
I am trying to make a UIButton Grid with respect to size of superView.Most of the solutions that I have seen are such where buttons are added to scrollView .But what if I don't want scrollable grid and just a normal grid .How can I resize my buttons depending on the size of its superview where they are plotted?.Has anyone tried it?Any help is really appreciated.
-(void)createLayout{
int width =30;
int height =30;
int leftMargin =10;
int topMargin =10;
int xTile;
int yTile;
int xSpacing = 50;
int ySpacing = 50;
for (int c=1; c<=5; c++) {
for (int r=1; r<=10; r++) {
NSLog(#"row : %d col : %d",r,c);
yTile = ((c*ySpacing)+topMargin);
xTile = ((r*xSpacing)+leftMargin);
Test *btn = [Test buttonWithType:UIButtonTypeCustom];
btn.row = r;
btn.column =c;
[btn setTitle:[NSString stringWithFormat:#"%d,%d",btn.row,btn.column] forState:UIControlStateNormal ];
[btn.titleLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[btn setBackgroundColor:[UIColor redColor]];
[btn setFrame:CGRectMake(xTile, yTile, width, height)];
[self.scrollView addSubview:btn];
xTile=xTile+10;
}
yTile = yTile+10;
}
}
Well to answer your question I wrote a code. Something like this will help you. I have created this project for you here. Check that. Also it's not completely fine I would like you to work on it and made a commit when you have made it perfect.
- (UIView *) view : (CGSize) viewSize withButtons : (int) numberOfButtons{
CGRect frame = CGRectZero;
frame.size = viewSize;
UIView *view = [[UIView alloc]initWithFrame:frame];//considering View size is after margine
CGSize buttonSize = [self getButton:numberOfButtons sizeFor:viewSize];
for (int i = 1; i <= numberOfButtons; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:[self getButton:buttonSize frameForIndex:i inView:viewSize]];
[button setTitle:[[NSNumber numberWithInt:i] stringValue] forState:UIControlStateNormal];
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[button.layer setBorderWidth:1.0f];
[view addSubview:button];
}
return view;
}
- (CGRect) getButton : (CGSize) buttonSize frameForIndex : (int) buttonIndex inView : (CGSize) containerSize{
int x = buttonIndex % (int)(containerSize.width / buttonSize.width);
x *= buttonSize.width;
int y = buttonIndex % (int)(containerSize.height / buttonSize.height);
y *= buttonSize.height;
CGRect frame = CGRectZero;
frame.origin = CGPointMake(x, y);
frame.size = buttonSize;
return frame;
}
- (CGSize) getButton : (int) numberOfButtons sizeFor : (CGSize) containerSize{
double containerArea = containerSize.width * containerSize.height;
double areaOfOneButton = containerArea / numberOfButtons;
double edgeOfOneButton = sqrt(areaOfOneButton);
edgeOfOneButton = (edgeOfOneButton > containerSize.width) ? containerSize.width : edgeOfOneButton;
return CGSizeMake(edgeOfOneButton, edgeOfOneButton);
}
I'm making a circle with different buttons all over, spaced out at equal intervals.
Here's what it looks like :
I want to make it so that the picture doesn't rotate. How do I achieve that ? Here's the code.
- (void)drawWheel
{
// Drawing the Wheel view
wheelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 275, 275)];
wheelView.center = self.view.center;
wheelView.layer.cornerRadius = wheelView.frame.size.width / 2.0;
wheelView.layer.borderColor = [UIColor whiteColor].CGColor;
//wheelView.layer.borderWidth = 0.5f;
CGFloat angleSize = 2 * M_PI / self.buttons.count;
for(int i = 0; i < self.buttons.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wheelView.frame.size.width / 2.0, 40)];
label.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
label.layer.position = CGPointMake(wheelView.bounds.size.width / 2.0, wheelView.bounds.size.height / 2.0);
label.transform = CGAffineTransformMakeRotation(angleSize * i);
label.backgroundColor = [UIColor clearColor];
UIButton *button = [self.buttons objectAtIndex:i];
button.center = CGPointMake(label.center.x, label.center.y + 15);
button.transform = CGAffineTransformRotate(label.transform, 2 * (angleSize * i));
[label addSubview:button];
[wheelView addSubview:label];
}
[self.view addSubview:wheelView];
}
I didn't check it, but could you change following code
button.transform = CGAffineTransformRotate(label.transform, 2 * (angleSize * i));
to
button.transform = CGAffineTransformMakeRotation(-1 * angleSize * i);
Here I am just rotating your button to opposite direction
You could do something like this I think...
- (void)drawWheel
{
// Drawing the Wheel view
wheelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 275, 275)];
wheelView.center = self.view.center;
wheelView.layer.cornerRadius = wheelView.frame.size.width / 2.0;
wheelView.layer.borderColor = [UIColor whiteColor].CGColor;
//wheelView.layer.borderWidth = 0.5f;
CGFloat angleSize = 2 * M_PI / self.buttons.count;
for(int i = 0; i < self.buttons.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wheelView.frame.size.width / 2.0, 40)];
label.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
label.layer.position = CGPointMake(wheelView.bounds.size.width / 2.0, wheelView.bounds.size.height / 2.0);
label.transform = CGAffineTransformMakeRotation(angleSize * i);
label.backgroundColor = [UIColor clearColor];
UIButton *button = [self.buttons objectAtIndex:i];
button.center = CGPointMake(label.center.x, label.center.y + 15);
// change this line
button.transform = CGAffineTransformMakeRotation(-angleSize * i);
[label addSubview:button];
[wheelView addSubview:label];
}
[self.view addSubview:wheelView];
}
Here I am just transforming the button in the other direction with the same angle.
i.e. if the label is rotated 35 degrees then the button is rotated -35 degrees. This will mean the button is rotated 0 degrees relative to the sup review of the label.
Hi I am able to see my UIScrollView offset the first time but I would like it to be constantly updating and checking the offset as the user scrolls from left to right.
Here is what I have.
My UIScrollView
int PageCount = 2;
NSMutableArray *myArray =[[NSMutableArray alloc]initWithObjects:#"12-4.png", #"13-4.png", nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled = YES;
scroller.backgroundColor = [UIColor clearColor];
scroller.pagingEnabled = YES;
[self.view addSubview:scroller];
width = scroller.frame.size.width;
xPos = 0;
for (int i = 0; i < PageCount; i++)
{
ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[myArray objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width += scroller.frame.size.width;
xPos += scroller.frame.size.width;
}
And then the offset
if (scroller.contentOffset.x >= 0 && scroller.contentOffset.x <= 320)
{
NSLog(#"Offset %f", scroller.contentOffset.x);
}
And that returns 0.I would like to constantly update and check the offset.
I have tried [scroller needsDisplay] and [scroller needsLayout]
Any help would be really appreciated.Thank you.
You need to learn the basics of C:
if (scroller.contentOffset.x >= 0 && scroller.contentOffset.x <= 320)
This assumes you want to see if the x offset is between 0 and 320 (inclusive).
To check the offset as the scroll view is scrolled, implement the scrollViewDidScroll: delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x >= 0 && scrollView.contentOffset.x <= 320) {
NSLog(#"Offset %f", scroller.contentOffset.x);
} else if (...) {
}
}
And of course you need to see the scroll view's delegate property.
I want to create 15 UIButtons progaramatically using for loop. I want to arrange buttons like matrix with rows and colums, say 3 rows and 5 columns. The buttons height is 50 and width is 80. I can set y,width,height coordinates. But I face problem only with x coordinate. Tell me the logic for setting x coordinate.
Thanks in advance.
float x = 10;
float y = 10;
float width = 50;
float height = 50;
for(int i =0;i<15;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(x, y, width, height)];
[btn setTitle:[NSString stringWithFormat:#"B%d",i+1] forState:UIControlStateNormal];
[self.view addSubview:btn];
x = x + width + 20;
if((i+1)%3==0)
{
x = 10;
y = y + height + 20;
}
}
Just replace x and y and also set height and width in above code.
int row = 0;
int column = 0;
for (int i = 0; i < 15; i++)
{
if((row%3 == 0) && (row > 0))
{
row = 0;
column++;
}
else{
row++;
}
CGRect btnFrame = CGRectMake(row*80+10, column*50+10, 80, 50);//your button frame
UIButton *btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
[btnTemp setFrame:btnFrame];
[self.view addSubView:btnTemp];
}
Here you go:
CGFloat marginX = 10.f;
CGFloat marginY = 5.f;
CGRect buttonFrame = CGRectMake(0., 0., 80., 50.f);
for(NSUInteger row = 0; row < 5; row++) {
for(NSUInteger column = 0; column < 3; column++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = buttonFrame;
//... more button settings
[self.view addSubview:button];
buttonFrame.origin.x += marginX;
}
buttonFrame.origin.x = 0.;
buttonFrame.origin.y += marginY;
}