programmatically created UIButton's position changing

Discussion in 'iOS Programming' started by erdinc27, Jan 13, 2014.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #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.
     

    Attached Files:

  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    If you say you've done this programmatically, then please post some code.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    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]])
            {
                
            
            }
        }
        
    }
    
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    What happens in this if-block?

    Also, what debugging, if any, have you done?
     
  5. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #5
    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?
     
  6. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #6
    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


    ----------

    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.
     
  7. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #7
    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.
     
  8. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #8
    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
     
  9. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #9

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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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.
     
  11. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    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.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Yeah, I haven't found it easy getting used to. And if you want to move things around, it just makes things so complicated.
     
  13. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #13
    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.
     

Share This Page