iOS programmatically created UIButton's position changing

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
hey guys. i have an UIScrollView and inside of it i created buttons programmatically and added an UIView(green view) in storyboard. my problem is when a button is tapped then the UIView(green view) will move under of that button and other buttons will move down. UIview's height changes according to the button that user taps. but i couldnt make a logic. can u help me about that ? i uploaded how my scrollview looks.
 

Attachments

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
here how i created the UIButtons
Code:
- (void) replaceTheGroups
{
    int btnY = 0;
    
    for (int i= 0; i< [self.arrObjects count]; i++)
    {
        GroupObject *grupObj = [self.arrObjects objectAtIndex:i];
        
        UIButton *btnGroup = [UIButton buttonWithType:UIButtonTypeCustom];
        btnGroup.frame = CGRectMake(0, btnY, 265, 46);
        btnGroup.titleLabel.text = grupObj.groupBaslik;
        [btnGroup setTitle:grupObj.groupBaslik forState:UIControlStateNormal];
        btnGroup.titleLabel.font = [UIFont fontWithName:@"AvenirNextCondensed-DemiBold" size:16];
        btnGroup.tag = i;
        [btnGroup addTarget:self action:@selector(grupButtonTappedwithObj:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrGruplar addSubview:btnGroup];
        
        btnY = btnGroup.frame.origin.y + btnGroup.frame.size.height + 14;
        
    }
    
    
}
and here the method that will work when a button tapped
Code:
- (void) grupButtonTappedwithObj:(UIButton *)button
{
    self.categoryView.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y +46, button.frame.size.width, [[[self.arrGroup objectAtIndex:button.tag] objectForKey:@"kategoriler"] count]*46);
    
    for (UIView *theView in self.scrGruplar.subviews)
    {
        if ([theView isKindOfClass:[UIButton class]])
        {
            
        
        }
    }
    
}
 

1458279

Suspended
May 1, 2010
1,601
1,520
California
my problem is when a button is tapped then the UIView(green view) will move under of that button and other buttons will move down.
You've said what it does that's a problem, what are you wanting it to do?

It's not clear what you are wanting this to do.

From what you say, you have a series of buttons and a green view. the green view is moving when you tap a button and I'm guessing you don't want that to happen.

So, what do you want to happen when a button is tapped?
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
What happens in this if-block?



Also, what debugging, if any, have you done?
i wrote some codes there and it works but i couldnt get the result that i want. that is why i left empty when i paste the codes here. i need a hint for it. the code looks like that actually
Code:
- (void) grupButtonTappedwithObj:(UIButton *)button
{
    self.categoryView.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y +46, button.frame.size.width, self.categoryView.frame.size.height);
    
    
    for (UIView *theView in self.scrGruplar.subviews)
    {
        if ([theView isKindOfClass:[UIButton class]])
        {
            if (button.tag < theView.tag)
            {
                NSLog(@"button tag is smalller");
            }
            else if (button.tag > theView.tag)
            {
                NSLog(@"button tag is bigger");
            }
            else
            {
                NSLog(@"tags are equal");
            }
        }
    }
    
}
and the log screen looks like this
tags are equal
button tag is smalller
button tag is smalller
button tag is smalller


----------

You've said what it does that's a problem, what are you wanting it to do?

It's not clear what you are wanting this to do.

From what you say, you have a series of buttons and a green view. the green view is moving when you tap a button and I'm guessing you don't want that to happen.

So, what do you want to happen when a button is tapped?
i want that view will move under the tapped button and the other buttons will change the position according to view's position. for example if the user taps second button then the green view will be under the second button and the first button will keep its position but 3rd and 4th buttons will move down according to the View's height. view's height changes because of its content.
 

1458279

Suspended
May 1, 2010
1,601
1,520
California
Ok, so when you get into the method after a button has been tapped, you need to:

1. find out which button was tapped
2. move or delete and rewrite the green view

The button will have it's position and size, so you can reset the green view position and size to that of the button that was tapped.

The logic for moving the other buttons could be a for loop (as you have) draw the buttons as normal until you get to the one that was tapped, then change the position after that.

A simple if... else should do that just fine.
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
thanks for replies guys. i did something like that and unchecked Autolayout. now it works
Code:
- (void) grupButtonTappedwithObj:(UIButton *)button
{
    CGFloat viewHeight = [[[self.arrGroup objectAtIndex:button.tag] objectForKey:@"kategoriler"] count] * BUTTON_HEIGHT;
    
    for (UIView *theView in self.scrGruplar.subviews)
    {
        if ([theView isKindOfClass:[UIButton class]])
        {
            if (theView.tag < button.tag)
            {
                theView.frame = CGRectMake(0, (BUTTON_HEIGHT * theView.tag), BUTTON_WIDTH, BUTTON_HEIGHT);
            }
            else if (theView.tag > button.tag)
            {
                theView.frame = CGRectMake(0, (BUTTON_HEIGHT * theView.tag) + viewHeight, BUTTON_WIDTH, BUTTON_HEIGHT);
            }
            else
            {
                theView.frame = CGRectMake(0, button.tag * BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
            }
        }
        else if ([theView isKindOfClass:[UIView class]])
        {
            self.categoryView.frame = CGRectMake(0, (button.tag * BUTTON_HEIGHT)+ BUTTON_HEIGHT, BUTTON_WIDTH, [[[self.arrGroup objectAtIndex:button.tag] objectForKey:@"kategoriler"] count]*BUTTON_HEIGHT);
        }
    }
    
    self.scrGruplar.contentSize = CGSizeMake(265, self.categoryView.frame.size.height + [self.arrGroup count]*46 + ([self.arrGroup count]-1)*14);
}
but when Autolayout is checked it doesnt work
 

1458279

Suspended
May 1, 2010
1,601
1,520
California
thanks for replies guys. i did something like that and unchecked Autolayout. now it works
Code:
- (void) grupButtonTappedwithObj:(UIButton *)button
{
    CGFloat viewHeight = [[[self.arrGroup objectAtIndex:button.tag] objectForKey:@"kategoriler"] count] * BUTTON_HEIGHT;
    
    for (UIView *theView in self.scrGruplar.subviews)
    {
        if ([theView isKindOfClass:[UIButton class]])
        {
            if (theView.tag < button.tag)
            {
                theView.frame = CGRectMake(0, (BUTTON_HEIGHT * theView.tag), BUTTON_WIDTH, BUTTON_HEIGHT);
            }
            else if (theView.tag > button.tag)
            {
                theView.frame = CGRectMake(0, (BUTTON_HEIGHT * theView.tag) + viewHeight, BUTTON_WIDTH, BUTTON_HEIGHT);
            }
            else
            {
                theView.frame = CGRectMake(0, button.tag * BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);
            }
        }
        else if ([theView isKindOfClass:[UIView class]])
        {
            self.categoryView.frame = CGRectMake(0, (button.tag * BUTTON_HEIGHT)+ BUTTON_HEIGHT, BUTTON_WIDTH, [[[self.arrGroup objectAtIndex:button.tag] objectForKey:@"kategoriler"] count]*BUTTON_HEIGHT);
        }
    }
    
    self.scrGruplar.contentSize = CGSizeMake(265, self.categoryView.frame.size.height + [self.arrGroup count]*46 + ([self.arrGroup count]-1)*14);
}
but when Autolayout is checked it doesnt work

That's not the 1st time I've heard autolayout didn't work well. I guess it's a work in progress :D
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
If you want to control the layout in code, you don't want Auto Layout. It's conflicting with your desire to change the layout.
It is possible to move things around using auto-layout, but it requires a change in mindset.

What you have to do is add constraints to your views and hook up IBOutlets to those constraints. (Or save an ivar to the constraint(s) if you create them in code.)

Then you change the constraints (usually the constant value(s) of the constraints and THAT changes the layout. You can even put a call to layoutIfNeeded in an animation block and that causes the changes to be animated.

I am forcing myself to use auto-layout lately. I hate it, to be honest. Everything is awkward and there are about 4 times as many steps to get anything done, but I figure that's the path Apple is leading us down, so it's time to learn it.
 

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
I am forcing myself to use auto-layout lately. I hate it, to be honest. Everything is awkward and there are about 4 times as many steps to get anything done, but I figure that's the path Apple is leading us down, so it's time to learn it.
Yeah, I haven't found it easy getting used to. And if you want to move things around, it just makes things so complicated.
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
It is possible to move things around using auto-layout, but it requires a change in mindset.

What you have to do is add constraints to your views and hook up IBOutlets to those constraints. (Or save an ivar to the constraint(s) if you create them in code.)

Then you change the constraints (usually the constant value(s) of the constraints and THAT changes the layout. You can even put a call to layoutIfNeeded in an animation block and that causes the changes to be animated.

I am forcing myself to use auto-layout lately. I hate it, to be honest. Everything is awkward and there are about 4 times as many steps to get anything done, but I figure that's the path Apple is leading us down, so it's time to learn it.
yeah Duncan u are exactly right. Apple leading us to use autolayout. but i didnt understand it very well and it seem i need time to understand it well. so in some small projects i use it as much as i can do. but that project will have only one orientation. that is why i thought i dont need autolayout.
 

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.