Resize UITableViewCell content when delete button shows up - objective-c

Is there any way to use autoresizing masks to move my content so that the delete button doesn't cover it up? Googling has told me that I need to set an autoresizing mask of UIViewAutoresizingFlexibleRightMargin on my subview. It seems to me like UIViewAutoresizingFlexibleWidth would actually make more sense; though I've tried them both and neither works.
The view that I am trying to shrink is just a label that is a subview of the cell's contentView. I am unsure if the contentView itself automatically resizes when the delete button shows up; but it seems like it isn't; otherwise my autoresizing mask should have worked.
If the presence of the delete button doesn't cause any views to be resized; is there anyway that I can do this manually?

You should use UIViewAutoresizingFlexibleLeftMargin.
Here's why. You want your contents to move to the left, basically making it seem like the delete button is pushing the contents to the left, out of it's way. flexibleLeftMargin basically means your UILabel will stay fixed to the right side of your contentView. The reason you want this, is because the delete button actually causes your contentView to shrink it's width.
The autoresizingmask of your UILabel refers to how it behaves inside the contentView, not the cell.
Give it a try, it should work.

This question is really old but I feel I should answer this anyway since I just found the solution myself.
Only the cell's ContentView gets resized with the confirmation button is shown. If you don't add your views (labels, imageviews, etc...) to the cell.contentView instead of adding them to the cell directly then they won't be resized when the contentView is resized. In my case, I was adding it to the cell directly.
So, instead of doing something like:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, width-10, 20)];
[nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
[nameLabel setHighlightedTextColor:[UIColor whiteColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[nameLabel setTag:101];
[cell addSubview:nameLabel];
[nameLabel release];
you should do:
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, width-10, 20)];
[nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
[nameLabel setHighlightedTextColor:[UIColor whiteColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[nameLabel setTag:101];
[[cell contentView] addSubview:nameLabel]; // <<--- note the change in this line!
[nameLabel release];
Hope this helps others who stumble upon this issue.

I am using iOS 7, I got the same issue. I am using a separate xib for the UITableViewCell with auto layout enabled, so just added one more constraint to the label so that it will have a fixed gap on its right side.

Related

How do you customise UISearchBar colors in iOS7?

I have searched quite a bit but cannot find a good answer to this.
I want to change the backgroundColor of the inner rounded view.
Like in Tweetbot on the search tap where it changes from gray to blue.
I understand that I probably need to iterate over the subviews but I don't know how to get the right object. (for the backgroundColor it's not the _searchLabel)
The default contrast of this element is so bad it's not even funny :(
Ok, this works. But note that you can't set a UIBarStyle beforehand or it will override everything.
https://stackoverflow.com/a/19836215/1252720
If you're still looking for a better answer, I just stumbled across this thread and found a great solution: UISearchBar text color change in iOS 7
If you look at the answer given by Sandeep-Systematix (not the accepted answer, but the answer right below), he mentions a really clean way to modify subviews in any class with this method:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
You can read more about this in Apple's documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
That said, here's what you'll need to change the white, rounded background of the UITextField inside the UISearchBar:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor redColor]];
Now if you needed to create different UISearchBars with different styles, you would simply create a subclass of UISearchBar and you'd end up with something like this:
[[UITextField appearanceWhenContainedIn:[MyCustomSearchBar class], nil] setBackgroundColor:[UIColor redColor]];
Use this code.
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubView:_searchBar];
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];

UITableViewCell Delete button screws up textLabel when using line break in text

I have a UITableViewCell, when swipe to bring up the Delete button it screws up the textLabel because I am using a line break in text.
Before:
In delete mode:
This is frustrating because I just want the delete button to go over my text, rather than trying to animate it out of the way which screws it up.
Why does this happen? Because I use a line break in my string?
How can I prevent this from happening?
Why does this happen? Because I use a line break in my string?
It happends because you've added the label to the contentView of the cell (which is good). When the delete button appears the contentView will be automatically resized and the UITableViewCell's textLabel will resize as well because it's autoresizingMask is set to UIViewAutoresizingFlexibleWidth. Unfortunately we cannot override the autoresizingMask of the default textLabel.
How can I prevent this from happening?
You'll have to build a custom cell yourself. Subclass UITableViewCell with a custom UILabel as a property and set it's autoresizingMask to UIViewAutoresizingNone.
//create and set your frame to whatever you want.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 110, 20)];
//set autoresizing
[label setAutoresizingMask:UIViewAutoresizingNone];
//add to view
[[self contentView] addSubview:label];
//set property to acces later
[self myLabel:label];
//release (if you aren't using ARC)
[myLabel release];
add some sample text to test it later on
[[cell myLabel] setText:#"MAX: 72.92 MPH Will fit!"];
If you're using interfacebuilder to create your view/cells make sure the autoresizing mask arrow's are grayed out.
You should be able to access the UITableViewCell's textLabel.Frame property. Subclass UITableViewCell and set this in [UITableViewCell didTransitionToState:(UITableViewCellStateMask)state]

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

reproduce UITableViewCellSeparatorStyleSingleLineEtched in UITableViewStylePlain

How can one reproduce the same effect as Single Line Etched when using plain table and custom cells?
I think I need to add them as subview to each cell, excluding the last one. I want to know how to reproduce that without having to use images to that. Does anyone know?
Isn't the separator just a single pixel grey line, even for Single Line Etched?
In this case, create a UIView the width of the cell, but only one pixel high and then set it's background colour,then stick it at the bottom of the cell's content view.
UIView *lineView;
lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
cell.contentView.bounds.size.height-1.0f,
cell.contentView.bounds.size.width,
1.0f);
lineView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];

How To Draw A Line Dividing The Cell's Accessory View

I just wanted to know how I could draw a line dividing the cell's accessory view from the rest of the cell.
You could simply add a thin UIView (or UIImageView if you wanted a fancier divider) like so.
UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(290, 3, 1, 38)];
[divider setBackgroundColor:[UIColor blackColor]];
[cell.contentView addSubview:divider];
[divider release];
If you're using one of UITableView's default accessory views, your content view will be squashed to make room for the accessory, so you your x-coordinate may have to change to sit at the very edge of the content view.