I am creating an iPhone app, now i am trying to align a uilabel inside custom tableview cell to the right using autoresizing mask, i am doing it in interface builder and i am using storyboards, but the problem i am having is that the label just disappears when i run app in iphone 6 simulator, the autoresizing is working fine when i align label to the left of table view cell, but strangely it is not working when i try to align the label right of table view cell, how should i do this?
Note: i don't need to align the uilabel text but need to align the uilabel itself to right.
the answer is used for my project , please customize the x, y , width and height as you need
I also face the same problem , SO i follow this method cellForRowAtIndexPath
cell.yourlableName.frame=CGRectMake(add your label x - position,add your label Y position, [[UIScreen mainScreen] bounds].size.width- add your label width, add your label Height ); for example in your iPhone 4 width is 320 so reduce the width 320-yourlabel width,
else part call this line in your custom cell class
- (void)awakeFromNib {
cell.yourlableName.frame=CGRectMake(180.0f,220.0f, [[UIScreen mainScreen] bounds].size.width-216.0f, 19.0f);
}
its working fine for me in all device it shows in right side perfectly.
Related
I am pretty new to iOS dev. I am trying to create a view with a UILabel and a UITableView. The UILabel is used to display some header text & the tableview is positioned below the header & it covers the remaining portion of the screen.
I am creating this view using code & not the storyboard approach. So I need to specify the height & width of my tableview as constraints. For the width I have set a constraint where it matches the width of the container view. However for the height I wish to use the following equation:
tableview's height = container view's height - UILabel's height
For this I need to know how I can get the container's & the UILabel's height via code. I tried the following approach-
CGFloat header_height=lblHeader.bounds.size.height;
But this gives the same height as the container view's height .i.e. 460. Can someone please explain how I can get the height of the UIlabel view?
To get the height of the UILabel do this instead:
CGFloat header_height = lblHeader.frame.size.height;
Frames and bounds are not the same.
Cocoa: What's the difference between the frame and the bounds?
I want to get my UITableViewController to scroll only when there is not enough room on the screen for all of its cells. I am using static cells that are designed to be shown in an iPhone 5, 6, and 6 plus size screen. However, when shown in the 4s screen, the bottom cells get cut off.
Ideally I would like to use AutoLayout to anchor the bottom of the tableview to the bottom of its superview as I have with other tableviews in my application, but Xcode doesn't allow me to add constraints to the UITableView in a UITableViewController. The reason I have to use the UITableViewController is because I am using a pod for a slide menu (https://github.com/SocialObjects-Software/AMSlideMenu) that subclasses UITableViewController.
I have tried to change the size of the UITableView's frame based on the screen size because I assumed a scroller would automatically be added if the cells took up more room than the containing frame. I used the following code to do so:
CGRect frame = self.tableView.frame;
[self.tableView setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [[UIScreen mainScreen] bounds].size.height - HEADER_HEIGHT)];
However, it had no effect on the way the table was displayed.
Does anyone know how I might be able to set the bottom of the UITableView dynamically and add a scroller to the cells when the screen is too small? I would also welcome any suggestions that might help avoid having to do this at all, as I would prefer not having to do anything too hacky.
You can set the alwaysBounceVertical property to false so the tableView will only scroll when its contentSize is larger than the table view's frame which you can constrain to your view however you like.
tableView.alwaysBounceVertical = NO;
UITableViewController height is determined by automatically calculating number of UITableViewCell you have presented. You can customize the cell height so that height of UITableViewController will automatically changed as you wished.
I'm trying to set up a simple UITableViewCell in Interface Builder without auto layout. I'm using the old springs and struts.
When I give a label a flexible width, it seems to lay out as if the UITableViewCell has a much larger width, even though the UITableViewCell tells me it has a width of 375 pixels in layoutSubviews:
- (void)layoutSubviews
{
[super layoutSubviews];
NSLog(#"Label width: %f", self.nameLabel.frame.size.width); // 695.0 for a label that stretches the whole width
NSLog(#"Superview (UITableViewCell) width: %f", self.nameLabel.superview.frame.size.width); // 375.0
}
On a simulated iPhone 5S (iOS 7 or 8), the superview is 320 but the UILabel spreads to 640.
On a simulated iPhone 6, the superview is 375 but the UILabel spreads to 695.
On a simulated iPhone 6 Plus, the superview is 414 but the UILabel speads to 734.
I don't have this problem with other views. For example, I'm able to add a UILabel to a UIViewController and have it stretch the width correctly. What is going on? And how do I fix this?
EDIT:
Interestingly, if I add the constraints programmatically during cellForRowAtIndexPath: then it seems to work as expected, so long as I use the older dequeueReusableCellWithIdentifier instead of dequeueReusableCellWithIdentifier:forIndexPath:. I want to keep all my constraints in Interface Builder though. Any ideas?
I played around a bit with this today. Looks like the frame of the label when it's instantiated relative to the frame of the content view is wrong. If I make the label the same size as the cell in the storyboard, then in awakeFromNib the contentView has a size of CGRectZero but the label has the size I set in the storyboard. Therefore, when you get to layoutSubviews and the contentView is resized to the right size (0,0,320,44), because of the flexible width mask, the label itself is resized along with the content view (width increases by 320 as well). That's why its appearing larger than intended.
The only way I could get around this (albeit, it feels like a gross hack and you should probably stick to auto-layout), was to set the label's frame relative to the content view in awakeFromNib.
- (void)awakeFromNib
{
[super awakeFromNib];
self.label.frame = self.bounds;
}
How can I adapt an UITableView height to iPhone 4-inch screen? I have the following configuration, which works for iPhone 4. However on iPhone 5 (see image below) there is a blank space because the tableview does not rezise. The tableview is scrollable.
I've autolaout enabled and I tried defining horizontal and vertical spacing constraints for all views and the tableview, but I get the same results (see image below). Any suggestion?
This viewcontroller is placed inside a placeholder since I created a custom menu. I tried settting the constant of the placeholder in the parent view with the following code. Also not working, the tableview is clipped.
-(void) viewDidLoad{
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
NSLog(#"iphone5");
self.dynamicTVHeight.constant = 465;
[self.placeholder needsUpdateConstraints];
}
else{
NSLog(#"iphone4");
self.dynamicTVHeight.constant = 375;
[self.placeholder needsUpdateConstraints];
}
And these are the constraints of my parentview, which implements a custom navigation menu. I did two tests:
With the vertical and horizontal spacing constraints:
And with the height constraint in my placeholder:
You need to set the bottomSpaceToContainer (or Bottom layout) to 0.
Make sure that there are no warnings or errors in your nib file (indicated by the yellow or red sign on your UITableView). If there are none, make sure you have all 4 constraints needed to fully describe your UITableView's position.
What you want :
To compare iphone 4 and iphone 5 :
Hope that will help =)
I want to have a text title scroll above a text view, but can't find information on how to do it. I've been looking for days, but can't even figure out how to look for the info. I want the function to work like the Apple Notes app where the date text is positioned above the text entry location and scrolls off the screen with the text but is not editable.
I've tried placing labels above UITextView, but the label does not scroll with the textView. I have a sample Xcode project I'm work with, but not sure if it can be uploaded for others to see what I'm doing. I almost had success with the project, but the labels only scroll with the text in the landscape view, not the portrait view for some reason.
I've read Apple developer docs on TextViews and several other sources without finding any discussion on how to do this or examples to follow.
Can anyone point me in the right direction?
I'm sure there must be a better way to do this, but this is the only approach I've found to work after more than a week of trying to find a solution. If I can figure out how to upload the entire Xcode example project I will do that.
This is the explanation of the approach I found to work under iOS 6.1 and Xcode 4.5. I have only tired this on the iPhone 6.1 simulator.
After creating a full screen text view and placing a label at the top of the text view,
To get the label to scroll off screen with the editable text view you need to:
turn off autolayout in interface builder (uncheck Use Autolayout). (there appears to be a problem with autoLayout and scrollViews in iOS 6.x)
turn off the text view scrolling in interface builder (uncheck Scrolling Enabled)
set the text view content insets height (Top) to the label height
embed the text view and label in a scroll view.
set properties for the scroll view and text view to be able to access their properties
in viewDidAppear:animated:
a. set the scroll view content size height to 20 plus the greater of the screen view height or the text view content height plus the label height
b. set the text view delegate to self
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
float screenHeight = self.view.bounds.size.height;
// set the scroll view content height to 20 plus the greater of the view height or the text view content size height plus the label height
screenHeight = MAX(screenHeight, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 20 + screenHeight);
// set the text view delegate
self.textView.delegate = self;
}
set the text view height dynamically in textView delegate method textViewDidChange:
a. if the text view content height is greater than the view bounds height less the label height:
1. set the text view frame to the view bounds width and the text view content height plus the lable height
2. set the scroll view content height to the view bounds width and the text view bounds height plus the label height
// dynamically set the text view content size height
-(void) textViewDidChange:(UITextView *)textView {
if (self.textView.contentSize.height > self.view.bounds.size.height - 20) {
self.textView.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.textView.bounds.size.height + 20);
}
}
set the struts and springs in interface builder
1. set the scroll view and text view struts for left, right and bottom
2. set the scroll view and text view springs for width and height
3. set the label struts for left, right and top
4. set the label spring for width
The same settings need to be made every time the view changes orientations.
There might be a way to do this by setting constraints in code under autolayout, but I barely understand constraints, much less setting them successfully in code.
Hope this helps someone else.
What you are looking for is called UIScrollView. Take a look at the Apple documentation here.
A good tutorial on UIScrollView can be found on the Ray Wenderlich site.