I have a UITableView with some contents. The cells have their own UITableViewCell class that has two UIView and a UILabel properties. When the user taps a cell, one of the views changes it's color. But if I want to return the previous color, it doesn't happen.
I tried with custom BOOL variable, with some methods inside the UITableViewCell class, everything - no success.
(Note: I tried with the UITableViewCellAccessoryType accessory (I don't want cell accessory, I just tried it) and it worked......)
So, how can I toggle a view/label/anything inside a cell? Thank you.
p.s. I can show you code, but it seems more theoretical question......
Usually you override setSelected: in your table view cell subclass and change to color there (depending on the selected state.
Like this:
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if (selected) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor whiteColor];
}
}
Related
i have searched a lot but i only find how to change the delete button. What i have to change is the color of the little rounded button. Is that possible?
That red button is not a button . It is a image.
You need to find for a subview in the UITableViewCell and when the subview is UITableViewCellEditControl, you cast the view as image view and you change the image in it.
You need to have a custom colored image similar to the red image.
for (UIView *subv in cell.subviews){
if ([NSStringFromClass([subv class]) isEqualToString:#"UITableViewCellEditControl"]) {
for (UIView *imgV in subv.subviews){ {
if(imgV isKindOfClass:[UIImageView class]]){
UIImageView *imgView = (UIImageView *)imgV;
imgView.image=[UIImage imageNamed:#"blue.png"];
imgV.backgroundColor=[UIColor blueColor];
NSLog(#"subview-subview name is %#",NSStringFromClass([imgV class]));
}
}
}
}
Please note that changing the private subviews is not a good practise. This method may not work in the next iOS update- credits to #rmaddy.
Another work around would be to design your own custom cell.
I am trying to create editable transparent NSTextField in a semi transparent window:
What I have noticed is that whenever the field is editable there is a white "selection like" background drawn even though the element is not actually selected.
Additional observable symptoms:
This highlight is not present when the field is set as non-editable.
If there are multiple fields only the first one has the highlight.
The highlight is not present if the text is not set programmatically
Following code was used to generate the field:
f = [[NSTextField alloc] initWithFrame:b2];
f.backgroundColor = [NSColor clearColor];
f.drawsBackground = YES;
f.bordered = NO;
f.bezeled = NO;
f.focusRingType = NSFocusRingTypeNone;
f.textColor = [NSColor whiteColor];
f.editable = YES;
f.selectable = YES;
f.backgroundColor = [NSColor clearColor];
f.allowsEditingTextAttributes = YES;
f.stringValue = #"Foo";
[self.contentView addSubview:f];
Additional observations (potentially a separate problem):
When field is not the first field on the screen and the initial text is set programmatically and removed by editing the field there is a shadow of the text:
I can't seem to find any documentation on this I wonder if any of you have had this happen and potentially have a solution or a pointer to docs I might have not stumbled upon.
part 1: removing highlight
there are two options here depending on the behavior you are looking for
option 1 - nil first responder
TextField is not first responder
No highlighted text
No Cursor at the end of text
Assuming you are using an NSWindow, set the first responder to nil after calling makeKeyAndOrderFront
[self.window makeKeyAndOrderFront:self];
[self.window makeFirstResponder:nil];
It appears as though makeKeyAndOrderToFront: looks for the first NSResponder in the window willing to accept first responder. Then becomeFirstResponder is called on that responder; leading to option 2
option 2 - override becomeFirstResponder
TextField is first responder
No highlighted text
Cursor appears at the trailing edge of text
Subclass NSTextfield and override it's becomeFirstResponder method
#implementation BPTextField
- (BOOL)becomeFirstResponder {
BOOL isResponder = [super becomeFirstResponder];
//Get Field editor, set selected range
NSText* fieldEditor = [[self window] fieldEditor:YES forObject:self];
[fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length ,0)];
return isResponder;
}
#end
I prefer this option from a usability perspective
part 2: removing shadow
option 1 - add a solid background color
I'm not clear ; ) on why this is the case, but if you add a solid background color, the text will update.
option 2 - override textDidChange
override textDidChange:notification in your textfield
#implementation BPTextField
- (void)textDidChange:(NSNotification *)notification {
[super textDidChange:notification];
[self setNeedsDisplay:YES];
}
#end
Final notes
You'll notice that the text looks bad, or rigid. Adding a background color to the textfield, or to the superview's layer will fix this.
This is an answer to part 2 of the question.
The shadow artifact is from rendering window's shadow which is not updated when the text in the NSTextField changes.
If the window's hasShadow method returns "NO" the text's shadow will not create shadow for the text either.
I've a custom subclass of NSOutlineView that use a TextFieldCell as cell. Some items that has child (that represents a group of child).
I want to have a custom color for the items with child and another custom color for the items without child. I tried to change it in IB but the color changes only for the child items and in code I can also set a custom color but only for the child items. Anyone can help me?
After a thorough research seems that the only way to do that is to override the drawRow: method on the NSOutlineView subclass
you can also implement NSOutlineViewDelegate method
- (void) outlineView:(NSOutlineView*)aTableView willDisplayCell:(id)aCell
forTableColumn:(NSTableColumn*)aTableColumn item:(id)item;
in it you could have a part that goes something along the lines of:
if ([self outlineView:self isGroupItem: item]){
NSColor *color;
if ([item.children count] == 0) {
color = [NSColor redColor];
}
else {
color = [NSColor blueColor];
}
[aCell setDrawsBackground:YES];
[aCell setBackgroundColor: color];
}
Sorry about formatting, i'm still rather new at this.
This should work, since I've used this code for something similar.
I hope that's what you are looking for.
My UICollectionView cells contain UILabels with multiline text. I don't know the height of the cells until the text has been set on the label.
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
This was the initial method I looked at to size the cells. However, this is called BEFORE the cells are created out of the storyboard.
Is there a way to layout the collection and size the cells AFTER they have been rendered, and I know the actual size of the cell?
I think your are looking for the invalidateLayout method you can call on the .collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling -collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them.
An example for the usage of invalidateLayout can be found here. Also consult the UICollectionViewLayout documentation on that method:
Invalidates the current layout and triggers a layout update.
Discussion:
You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.
Edit:
For storyboard collection view which contains auto layout constraints, you need to override viewDidLayoutSubviews method of UIViewController and call invalidateLayout collection view layout in this method.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[yourCollectionView.collectionViewLayout invalidateLayout];
}
subclass UICollectionViewCell and override layoutSubviews like this
hereby you will anchor cell leading and trailing edge to collectionView
override func layoutSubviews() {
super.layoutSubviews()
self.frame = CGRectMake(0, self.frame.origin.y, self.superview!.frame.size.width, self.frame.size.height)
}
Hey in the above delegate method itself, you can calculate the UILabel size using the below tricky way and return the UICollectionViewCell size based on that calculation.
// Calculate the expected size based on the font and
// linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(9999,9999);
CGSize expectedLabelSize =
[[self.dataSource objectAtIndex:indexPath.item]
sizeWithFont:[UIFont fontWithName:#"Arial" size:18.0f]
constrainedToSize:maximumLabelSize
lineBreakMode:NSLineBreakByWordWrapping];
- (void)viewDidLoad {
self.collectionView.prefetchingEnabled = NO;
}
In iOS 10, prefetchingEnabled is YES by default. When YES, the collection view requests cells in advance of when they will be displayed. It leads to crash in iOS 10
I have set background colors for all my UITableViewCells. However, when I click my UIBarButtonItem "edit", the delete and the draggable icons distort the background color, making white background behind them. Is there any way around this? I can show code if necessary, but this seems like a pretty straightforward question.
The background color of a table cell is usually set like this:
cell.backgroundView.backgroundColor = [UIColor redColor];
This works fine. However, when an object of class UITableViewCell is created, by default it does not have a backgroundView, it is nil. The developer needs to create the backgroundView when needed. So depending on the particular situation you need to do something like:
if (cell.backgroundView == nil) {
cell.backgroundView = [[[UIView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [UIColor redColor];
This is the default behavior of the uitableviewcells in the editing mode. Try setting the background color for your tableViewCells again in
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
// set background color
}
else {
}
If needed, try setting your background color as your property so that you can set it up here.