Change tint of SFSymbol in UITableView Cell Accessory tvOS - cocoa

I am adding an accessory view to a cell with:
UIImageSymbolConfiguration* configuration = [UIImageSymbolConfiguration configurationWithPointSize:28 weight:UIImageSymbolWeightRegular];
UIImageView* accessoryView = [[[UIImageView alloc] initWithImage:[UIImage systemImageNamed:#"globe" withConfiguration:configuration]] autorelease];
[accessoryView setImage:[imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
[accessoryView setTintColor:[UIColor labelColor]];
[cell setAccessoryView: accessoryView];
This works, but how can I configure it so that when the cell is highlighted, the globe icon changes from the labelColor tint to black (so it matches the text color)?
I have also tried making a highlighted image but it does not have any effect:
UIImageSymbolConfiguration* configuration = [UIImageSymbolConfiguration configurationWithPointSize:28 weight:UIImageSymbolWeightRegular];
UIImageView* accessoryView = [[[UIImageView alloc] initWithImage:[UIImage systemImageNamed:#"globe" withConfiguration:configuration]] autorelease];
UIImage* normalImage = [accessoryView.image imageWithTintColor:[UIColor labelColor]];
UIImage* highlightImage = [accessoryView.image imageWithTintColor:[UIColor blackColor]];
[accessoryView setImage:normalImage];
[accessoryView setHighlightedImage:highlightImage];
[cell setAccessoryView:accessoryView];
Furthermore, the highlighted image never seems to be used at all:
UIImage* image = [[UIImage systemImageNamed:#"checkmark" withConfiguration:configuration] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImage* highlightedImage = [[UIImage systemImageNamed:#"globe" withConfiguration:configuration] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView* accessoryView = [[[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage] autorelease];
[cell setAccessoryView:accessoryView];
In the above code, the highlighted image is never shown.

This seems to work:
-(void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
NSIndexPath* nextPath = [context nextFocusedIndexPath];
if (nextPath)
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:nextPath];
UIImageView* accessoryView = (UIImageView *)[cell accessoryView];
[accessoryView setTintColor:[UIColor blackColor]];
}
NSIndexPath* prevPath = [context previouslyFocusedIndexPath];
if (prevPath)
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:prevPath];
UIImageView* accessoryView = (UIImageView *)[cell accessoryView];
[accessoryView setTintColor:[UIColor labelColor]];
}
}

Related

UICollectionView adding image and button

I used UICollectionView for displaying the images from CameraRoll, it is working fine, now I want to add the Camera button in the collectionview cell. I had written the code like this, button is not getting displayed.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
[self.cameraRoll thumbAtIndex:indexPath.row completionHandler:^(UIImage *thumb) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
//Now Create Button
UIImage* img = [UIImage imageNamed:#"btn_take_pic"];
UIImageView *btnImage = [[UIImageView alloc] initWithImage:img];
btnImage.frame = CGRectMake(0,0, 50,50);
btnImage.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
btnImage.contentMode = UIViewContentModeScaleAspectFill;
//End of create button
[cell.contentView addSubview:btnImage];//Add Button to the cell
}];
return cell;
}
Create Button like this
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
//Add your image showing code here
//Now Create Button in cell
CGRect btnRect = CGRectMake(0, 0 , 30 , 30);
UIButton *cellBtn = [[UIButton alloc] initWithFrame:btnRect];
[cellBtn setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[cellBtn setTitle:#"Text to Show" forState:UIControlStateNormal];
[cell.contentView addSubview:cellBtn];
[[cell cellBtn] addTarget:self action:#selector(CellBtnTapped:event:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
method for button tap
-(void)CellBtnTapped:(id)sender event:(id)event
{
// perform your action here that you want to do on button tap
}
But It is good that you should make a separate class for your CollectionViewCell and add all objects there, that you want to take in your collectionView cell.
This is the solution for the issue I mentioned above, I think it might be helpful for others, so positing the code here
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"collectionCell" forIndexPath:indexPath];
if(indexPath.row>0)
[self.cameraRoll thumbAtIndex:(indexPath.row-1) completionHandler:^(UIImage *thumb) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:thumb];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
}];
else{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_take_pic.png"]];
imageView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:imageView];
}
return cell;
}

Adding UISegmentedControl to UIScrollView programmatically

I am tyring to add UISegmentedControl to UIScrolview programmatically.
I am using below code..
I don't why the scrollview is not enabled.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
scroll.contentSize = CGSizeMake(320, 700);
scroll.showsHorizontalScrollIndicator = YES;
NSArray *menu = [NSArray arrayWithObjects:[UIImage imageNamed:#"Accountimg.png"],[UIImage imageNamed:#"Challengesimg.png"],[UIImage imageNamed:#"Profileimg.png"],[UIImage imageNamed:#"Challengesimg.png"],nil];
UISegmentedControl *menuSegmentedControl = [[UISegmentedControl alloc] initWithItems:menu];
menuSegmentedControl.frame = CGRectMake(0,0,450,40);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[scroll addSubview:menuSegmentedControl];
[menuScrollView addSubview:scroll];
[menuSegmentedControl release];
[scroll release];
I am unable to scroll horizontally..
Thanks in advance
UPDATE:
I am able to get the horizontal scroll.
Code updated:
menuSegmentedControl.frame = CGRectMake(0,0,500,35);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
menuScrollView.contentSize = CGSizeMake(menuSegmentedControl.frame.size.width, menuSegmentedControl.frame.size.height);
menuScrollView.showsVerticalScrollIndicator = NO;
[menuScrollView addSubview:menuSegmentedControl];
menuScrollView.clipsToBounds = YES;
Thank you.

How can we put two line in UIBarButtonItem in Navigation Bar

"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Now Playing"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
So i want to pu line break in between "Now Playing". So please help me out.
Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(#"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.
- (void)createCustomRightBarButton_
{
UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
addCustomLabel.text =#"Now\nPlaying";
addCustomLabel.textColor = [UIColor whiteColor];
addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
addCustomLabel.numberOfLines = 2;
addCustomLabel.backgroundColor = [UIColor clearColor];
addCustomLabel.textAlignment = UITextAlignmentCenter;
CGSize size = addCustomLabel.bounds.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[addCustomLabel.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)]
autorelease];
}
You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;
-(void) viewDidLoad
{
//...
self.customButton.titleLabel.numberOfLines = 2;
self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
which in my case becomes
I create 2 PNG images by myself, and it looks good.
UIImage *img = [UIImage imageNamed:#"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:#selector(presentNowPlayingMovie)] autorelease];

How do I change the color of a UINavigation leftBarButtonItem

I want my leftBarButtonItem to be a red button. I haven't figured out how to do that. I'm using the following code to construct the navigation bar and button.
Any help is appreciated.
lq
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc]
initWithTitle:#"Clear"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
navItem.leftBarButtonItem = clearButton;
[navBar pushNavigationItem:navItem animated:false];
[navBar setDelegate:self];
[navItem release];
[clearButton release];
Maybe you will find a nicer solution in this posting at Stack Overflow or you can go the way using a UISegmentedControl
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Clear", nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor redColor];
...
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithCustomView:button];
navItem.leftBarButtonItem = clearButton;

xcode iphone - jerky scroll UITableView CellForRowAtIndexPath

Almost sorted with my 1st app, just a simple news app but when I load it onto my iPhone the scroll seems jerky can someone have a look at my function and see if i'm doing something wrong.
I need the image on the right hand side thats why i'm using custom cells.
Thanks
For any help
#define DATELABEL_TAG 1 #define MAINLABEL_TAG 2 #define PHOTO_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MainNewsCellIdentifier = #"MainNewsCellIdentifier";
UILabel *mainLabel, *dateLabel;
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MainNewsCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,15.0,170.0,15.0)] autorelease];
dateLabel.tag = DATELABEL_TAG;
dateLabel.font = [UIFont systemFontOfSize:10.0];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; //| UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:dateLabel];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,28.0,170.0,60.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont boldSystemFontOfSize:14.0];
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
mainLabel.numberOfLines = 0;
//mainLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:mainLabel];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(190.0,15.0,85.0,85.0)] autorelease];
photo.tag = PHOTO_TAG;
photo.contentMode = UIViewContentModeScaleAspectFit;//UIViewContentModeScaleAspectFit; //
[cell.contentView addSubview:photo];
}
else {
dateLabel = (UILabel *)[cell.contentView viewWithTag:DATELABEL_TAG];
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}
NSUInteger row = [indexPath row];
NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];
NSString *title = [stream valueForKey:#"title"];
NSString *titleString = #"";
if( ! [title isKindOfClass:[NSString class]] )
{
titleString = #"";
}
else
{
titleString = title;
}
CGSize maximumSize = CGSizeMake(180, 9999);
UIFont *dateFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize dateStringSize = [titleString sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:mainLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(15.0, 28.0, 170.0, dateStringSize.height);
mainLabel.frame = dateFrame;
mainLabel.text = titleString;
dateLabel.text = [stream valueForKey:#"created"];
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
photo.image = newsImage;
[imageURL release];
[newsImage release];
return cell;
}
The problem is this:
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
You effectively say here, that as soon as the cell needs to be displayed, the image must be fetched and presented. This will cost some time - too much for a good user experience.
You should fetch the images before or while you present the table view, and cache them, e.g. in an array. Or you must handle things asynchronously, meaning that you do the loading in the background, and not wait with return cell; until the image is actually downloaded. This will be a little harder to get right.
Even if you asynchronously download images, you'll still have a jerky scroll.
Why? It's lazy image decompression. ios performs decompression at the moment it will be displayed on the screen. You have to manually decompress the images in a background thread.
Decompression does not merely mean instantiating a UIImage object. It can be somewhat complicated. The best solution, is to download SDWebImage and use the image decompressor that's included. SDWebImage will asynchronously download and perform decompression for you.
To read more about the issue see: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

Resources