0

I have two labels in my View1. One of the label is multiline. First label is changing sometimes one line, sometimes more lines. Space between first and second label is fine with constraints. Problem is that second label always the same origin.y. After second label I need add button with code:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, second.frame.origin.y+ 10, 50, 100)];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:11]];
        [btn setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        [btn setTitle:@"DATA" forState:UIControlStateNormal];
        [btn sizeToFit];

        [view1 addSubview:btn];
WildWorld
  • 517
  • 1
  • 5
  • 18
  • 1
    What exactly are you asking here? Do you need a method to determine the y of your button based on the height of your second `UILabel`? – halileohalilei Jul 28 '15 at 08:49
  • I wonder why it does not change y of second label. – WildWorld Jul 28 '15 at 08:54
  • 1
    I don't know why you need to add this button programmatically. I think a better solution is add this buton to your storyboard and change its `height` with a constraint. (if you are **NOT** dealing with a `UITableView` or `CollectionView` cell) – Ersin Sezgin Jul 28 '15 at 08:55
  • Why are you messing with frames if you're using auto layout? – Robert J. Clegg Jul 28 '15 at 08:55
  • `second.frame.origin.y` gives you the top left point of your UILabel's frame. You need to use `second.frame.origin.y + second.frame.origin.height` to get the position you wish. Check the Core Graphics documentation for further info: https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/tdef/CGRect – halileohalilei Jul 28 '15 at 08:58
  • I need programmatically add button. Sometimes one button, sometimes four button. I don't know how to solve with auto layout. – WildWorld Jul 28 '15 at 09:00
  • 1
    Another thing is, if you're using constraints on your storyboard and not in your code, you're probably going to run into problems in the future. You can add constraint programmatically just as you would using the Interface Builder. Here's Apple's documentation for handling constraints programmatically: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html – halileohalilei Jul 28 '15 at 09:01

1 Answers1

0

Multiline always work if you set all the constraints properly either using storyboard or programatically but you have to set the constraints, In your case there some elements the UI and one created programatically so you have to set all the constraints programmatically. You can refer stack-overflow for how to set constraints programmatically. here is one

Community
  • 1
  • 1
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84