How to set a UILabel as a percentage of the width of the UICell with autolayout
I’m wondering how to set a UILabel’s width expressed as a % of the Cell’s total width? I’m using AutoLayout. Normally I would click on the label and set the EqualsWidth to a parentView and then adjust the multiplier so I set the width as a % of the parentWidth. But using InterfaceBuilder that option does not seem to be available within the Cell.
However I’m wondering maybe it has something to do with setting the preferred Width: I’m using ios8+ and have read that implicit widths are not available for ios8?
What I am hoping to achieve is that Label 1 and Label 3 are both 15% the width of the Cell and Label 2 has leading and trailing edges pinned to UIImageView and Label 3 that will allow it to grow and shrink depending on the width of the device.
- how to put constraints on views in interface builder to satisfy all iphone Screen in Xcode 8
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- Autolayout aspect ratio for UIImageView / UIView
- UILabel text not automatically resized using Auto Layout
- Can the height of the UISearchbar TextField be modified?
- Adding a dynamically sized view (height) to a UIScrollView with Auto Layout
2 Solutions Collect From Internet About “How to set a UILabel as a percentage of the width of the UICell with autolayout”
Steps you should do to achieve your goal of setting percentage width of any subview inside cell:
Add UIView to your cell and pin it to every edge – we have to add custom container view in order to be able to add “Widths equally” pin later on.
Add UILabel to previously added UIView container. Lets say we want to add left side label, so add 3 pins to it: left, top, bottom.
You can see now that you have error in your constraint setup. So lets fix it and add “Widths equally” pin. Select 2 views in View Controller Scene menu – the container and the label. Go to XcodeMenu/Editor/Pin/ and select “Widths equally”. Now you should see no errors in you constraints setup and the label should have same size as the container. (Please note that sometimes you have to refresh frames of your views after setting constraints, so if you see “yellow” warning icon don’t be afraid and just click on it).
Now if you have all your constraints we need to set actual percentage value. Open tab Size Inspector, click “Edit” title on constraint added in step 3. and change Multiplier value to 0.15 (15%).
Please note that, if you would add label using same technique but for right side, then you would need to set Multiplier value to 6.66 (= 1.00 / 0.15).
You can set width constraint and then create an
IBOutlet for that in
UITableView cell and change
constraint.constant value in
layoutSubView method of
- Single quotes around IBAction
- Creating a tutorial for my iOS app with page control
- How to set tcp_nodelay in GCDAsyncsocket?
- How can I change the language of the displayed UIKeyboard from within my iPhone app?
- Apple Push Notification Registration & Device Token Receive clarification?
- constrain view.bottom to superview.bottom not bottomLayoutGuide in storyboard
- How to build and deploy an ios api file in real device using MAC
- Why retainCount = 2 – after release?
- How to run NSTimer in background beyond 180sec in iOS 7?
- Save image to photo library using photo framework
- How to manage the gap between two views in auto layout
- Google SignIn without CocoaPods
- Objective C UITableView – Table cells display wrong content after changing cell's height
- How to handle NSData and return UIImage (Swift)
- Compiling mexopencv in OS X 10.9 with Xcode 5 and Matlab R2013b