0

I have to manage my UIButton position at the right side of a UITableViewCell like an image below.

enter image description here

in this cell I gave all my constraints from storyboard except of button..because I created button at runtime like below

In tableview:cellForRowAtIndexPath method

let mybutton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
var width:CGFloat = UIScreen.mainScreen().bounds.width
mybutton.frame = CGRectMake(width - 108, 8, 100, 31)
mybutton.tag = indexPath.row
cell?.contentView.addSubview(mybutton)  

So the problem is when we launch the app in portrait its ok but when we rotate it, button displays at portrait position...for e.g. if the button position at portrait 220 then in landscape it displays at 220 and after scrolls it looks ok because of cell reusability...

To solve this I'll trying to manually add few constraints to button.I don't know much about how to add constraints programatically but i'll add constraints like below one for top position..and similar with trailing

cell?.contentView.addConstraint(  
      NSLayoutConstraint(item:imageview ,  
                          attribute: NSLayoutAttribute.Top,  
                          relatedBy: NSLayoutRelation.Equal,  
                          toItem:mybutton ,  
                          attribute:NSLayoutAttribute.Top,  
                          multiplier: 1, constant: 8))  

but it displays breaking constraints...

So my questions are...

  1. How to deal with storyboard + manually constraints

  2. How to position button at the right side of a cell

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • Kindly try to solve this via storyboard. It is painful to maintain constraints in written in code. Kindly read about unified storyboard http://developer.xamarin.com/guides/ios/platform_features/intro_to_unified_Storyboards/ to have a custom constraint when device is on different orientation. – objectiveCarlo May 21 '15 at 06:41
  • i also want to do it like that...but the problem is to maintain state of the button ...i have to add button programatically because of cell reusability..so now i have to add constraints to button programatically... – Bhavin Bhadani May 21 '15 at 06:43
  • For cell reusability you can put your button inside a cell in a xib and you can also use constraints on xibs and it is cleaner. – objectiveCarlo May 21 '15 at 06:46
  • @Bhavin for cell reusability you can show/hide this button depends on the condition rather than adding programatically each time. Then you can manage the constraint in StoryBoard. – Akhilrajtr May 21 '15 at 06:48
  • @objectiveCarlo yes but now...i'll done all the thing except this one...so i want solve it with this style... – Bhavin Bhadani May 21 '15 at 06:49
  • @Akhilrajtr ...at first i try what you said...change image based on condition...but its not working...i don't know why? – Bhavin Bhadani May 21 '15 at 06:51
  • can you post the code that you've tried to change image with condition? correcting that may be the best solution than adding constraint programatically, since here we have other option. – Akhilrajtr May 21 '15 at 06:53
  • @Bhavin check [this](http://stackoverflow.com/a/30367138/2955078) answer to that question. – Akhilrajtr May 21 '15 at 07:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78383/discussion-between-bhavin-and-akhilrajtr). – Bhavin Bhadani May 21 '15 at 07:27

2 Answers2

0

I normally use the Auto-layout Visual Format

   //Horizontal constraints
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[button]", options: nil, metrics: metrics, views: views)
self.view.addConstraints(horizontalConstraints)

//Vertical constraints
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[button]", options: nil, metrics: metrics, views: views)
self.view.addConstraints(verticalConstraints)

Basically you set V or H for vertical and horizontal fallow by the UI item "|" for the board of the screen, than the -value in pixels- and finally the next [UIComponent]

You can find a nice tutorial here

Icaro
  • 14,585
  • 6
  • 60
  • 75
0

I have not enough reputation for comment that's why i am answering the question

You can reload Data after orientation changed.

It will solve the problem

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    tableVIew.reloadData()
}
iDhaval
  • 3,175
  • 1
  • 11
  • 21
  • lets say it works...i know it should be but its not a proper way...because as tableview reloads button gives flicker effect...and its horrible experience for user... – Bhavin Bhadani May 21 '15 at 07:21
  • I'm new to this and it's the only way I know. Any other suggestions are most welcome. – iDhaval May 21 '15 at 12:26