Changing the tint of the iOS UITextField clear button in iOS 11 - uitextfield

In iOS 10 we changed the tint of our UITextField clear buttons using the following KVO approach.
let clearButton = registrationTextField.value(forKey: "_clearButton") as! UIButton
if let clearImage = clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate) {
clearButton.setImage(clearImage, for: UIControlState())
clearButton.tintColor = UIColor.white
}
However, with the introduction of iOS 11 clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate) now returns nil.
More specifically clearButton.imageView?.image returns nil.
Are there alternative ways of accessing the image now?

Related

iOS 13 - UIPopoverPresentationController sourceview content visible in the arrow

When I am displaying some view in UIPopoverPresentationController and presenting it as popover
popoverCon?.modalPresentationStyle = UIModalPresentationStyle.popover
the content have moved upward toward and a some part is being display in the arrow.
Further I had border around the popover
popoverCon?.view.layer.borderColor = .orange
popoverCon?.view.layer.borderWidth = 1.0;
popoverCon?.view.layer.cornerRadius = 10.0;
popoverCon?.view.layer.masksToBounds = false;
it is not showing toward the part where arrow is but it displays a little of the border line in the tip of the arrow.
This was working fine until iOS 12 but in iOS 13 this issue is coming.
Any suggestions on how I can solve this?
The top of my tableView content was cut off by the arrow. This is how I fixed it in my case (code inserted in my tableViewController Swift file):
override func viewSafeAreaInsetsDidChange() {
if #available(iOS 11.0, *) {
super.viewSafeAreaInsetsDidChange()
self.tableView.contentInset = UIEdgeInsets(top: self.tableView.safeAreaInsets.top, left: 0, bottom: 0, right: 0)
}
}
My solution in Obj-C, for those who need an obj-c solution.
I had previously only popovercontroller, that was creating the error like shown in the question. I renamed it to childController for clarity and created a containing popoverController to make the solution given by #SaintMSent work in my situation of only one view originally. Also used https://stackoverflow.com/a/47076040/2148757 solution and https://useyourloaf.com/blog/self-sizing-child-views/ to resize appropriately since all of my childControllers set the preferred content size frequently.
//Create container popover controller and add child to it
UIViewController* popoverController = [[MyParentPopoverController alloc] init];
[popoverController.view addSubview:childController.view];
[popoverController addChildViewController:childController];
[popoverController setPreferredContentSize:childController.preferredContentSize];
//set popover settings on container
popoverController.modalPresentationStyle = UIModalPresentationPopover;
popoverController.popoverPresentationController.sourceRect = sourceRect;
popoverController.popoverPresentationController.sourceView = buttonView;
popoverController.popoverPresentationController.permittedArrowDirections = direction;
//Fix ios13 'bug' that Apple claims is a feature
UILayoutGuide* guide = popoverController.view.safeAreaLayoutGuide;
childController.view.translatesAutoresizingMaskIntoConstraints = NO;
[childController.view.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[childController.view.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[childController.view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[childController.view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
[popoverController.view layoutIfNeeded];
//Show the popover
...
#interface MyParentPopoverController : UIViewController
#end
#implementation MyParentPopoverController
-(void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container {
[super preferredContentSizeDidChangeForChildContentContainer:container];
[self setPreferredContentSize:container.preferredContentSize];
}
#end
Note: I didn't check for ios11 compatibility because my user base is restricted to not use it.
It is definitely a feature, they want you to use safe area since iOS 11, actually, but it seems now they want to force you to use it
Had the same problem as you, this worked for me
https://useyourloaf.com/blog/safe-area-layout-guide/
Definitely a bug. When you have a situation where you use UIPopoverArrowDirectionAny you will see that the problem only exists when the arrow is at the top or left of the popover and not when the arrow appears at the right or the bottom of the popover. If you make adjustments in your code to compensate it will work if you use UIPopoverArrowDirectionUp or UIPopoverArrowDirectionLeft but will not display correctly using that adjustment when using UIPopoverArrowDirectionAny and the popup appears above or to the right of the target rectangle.
I don't have an 'answer' yet, but I have identified what's going on and why it's so hard to fix.
ios13 UIPopoverViewController showing UITableViewController - Safe Area problems / Missing parts of table
Basically, any UITableView that has headers or footers is going to be broken in iOS 13 unless there's some way to alter the _UITableViewHeaderFooterViewBackground
That is notoriously problematic and doesn't play nicely with Auto-Layout - it's been known about for years, but Apple have never fixed it or made it easier to deal with and more publicly known.
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=video&cd=1&cad=rja&uact=8&ved=0ahUKEwibouuozfvkAhVCXRUIHVGsBegQtwIIKjAA&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DajsCY8SjJ1Y&usg=AOvVaw3_U_jy9EWH2dJrM8p-XhDQ
https://spin.atomicobject.com/2016/10/12/auto-layout-uitableview/
I'm unable to push my app to the App Store until I get this sorted.. I hope someone can identify how to manipulate this view so that it stops pushing the boundaries of the table out of whack with AutoLayout which causes this safe area intrusion.
Searching on the internet I got help from following link
Twitter
so I had to add safe area and manage my views accordingly
CGFloat topPadding = 0.0;
if (#available(iOS 11.0, *)) {
topPadding = self.view.safeAreaLayoutGuide.layoutFrame.origin.y;
}
Swift:
var topPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
topPadding = self.view.safeAreaLayoutGuide.layoutFrame.origin.y
}
but I haven't got solution to the border problem of mine yet.
Edit:
Temporarily I did solved the border problem by creating an invisible view on popover and giving it same frame as safe area and drawing its border.
You should use constraints. And also pay attention to topAnchor. It must be safeAreaLayoutGuide.topAnchor. In my case, it works correctly. For example:
[NSLayoutConstraint activateConstraints:#[
[toolbar.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
[toolbar.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
[toolbar.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[toolbar.heightAnchor constraintEqualToConstant:50]
]];
Embed the contents of the popover in another view with "safe area relative margins" on. This should have -21,-21 as the origin. Turn off vertical and horizontal auto resizing. Seems to work, although you lose auto-stretching.
Setup your popover's content UIViewController like such:
NSLayoutConstraint.activate([
myContentView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
myContentView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
myContentView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
myContentView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
])

How do I remove the focus shadows from a UIView in a UICollectionView in swift?

I'm working on a tvOS project in swift and have a horizontally aligned UICollectionView where each UICollectionViewCell is a custom UIView consisting of a UIImageView and a Label below it. So my UICollectionView looks like this:
| UIImageView | | UIImageView |
| Label | | Label |
I also wanted the image to grow/shrink as the particular view gained and lost focus. In my collection view I added the code
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
And in my custom cell class I have the line
imageView.adjustsImageWhenAncestorFocused = true;
So this somewhat works, although with 2 problems.
It seems all the views in the collection have a weird shadow under them, even when they're not focused. And then when they are focused, the image grows as expected, but it has another shadow under it. You can see what I mean here:
The first item in the collection has the focus
How do I get rid of them? I've tried all of these but none of them seem to work
contentView.layer.shadowOpacity = 0.0 // My imageView is a subview of this
contentView.layer.shadowColor = UIColor.clearColor().CGColor
imageView.layer.shadowOpacity = 0.0
imageView.layer.shadowColor = UIColor.clearColor().CGColor
It's not possible right now. I forwarded this question to Apple, and the response was: https://forums.developer.apple.com/message/105045#105045
I found a solution for UITableViewCell and CollectionViewCell:
self.clipToBounds = true
self.layer.cornerRadius = 10
This is if you needed to restore rounded edges.

iOS8 How to set TabBarItem images

It seems something has changed with iOS8 and now none of my tab bar icons are showing up properly. Most of the time they don't show until the tab is active:
But sometimes they don't show up at all and give me just a big blue box (like whenever I dismiss a view that covered the whole window):
This is what I did pre iOS8:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"paintbrush-black.png"]];
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
tabBarItem1.title = #"";
as mentioned, if you take a look at:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITabBarItem_Class/index.html#//apple_ref/occ/instm/UITabBarItem/setFinishedSelectedImage:withFinishedUnselectedImage:
you will notice that this method is deprecated, try to change:
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"paintbrush-black.png"]];
to:
[tabBarItem1 setImage:[[UIImage imageNamed:#"paintbrush-white.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"paintbrush-black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
You may also have problems with the image size, depends of the size of image when testing in iPhone 5 screen and iPhone 6 screen for #2x images
Did you try setSelectedImage:?
UIImage *image = [UIImage imageNamed:#"img.png"]
[tabItem setSelectedImage:image];
It works on my part.
This method is deprecated in iOS 8:
Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal

How to set the height in the code of the UITextView created with Xcode storyboard

I can not set the height while running of the UITextView object in Xcode for iPhone (Version 4.6 (4H127).
I have used MainStoryboard_iPhone.storyboard to create the UITextView.
I have tried this, which makes no effect:
UITextView *textMultiLine1;
[textMultiLine1 setContentSize:CGSizeMake(320, 320)];
Thanks for answering, I am a very beginner in Xcode.
You first need to create an Outlet in you header file:
IBOutlet UITextView *textMultiLine1;
Then you need to connect it in the StoryBoard:
Then you must set it's frame:
textMultiline1.frame = CGRectMake(originX, originY, width, height);
Or, if you want to change only it's height:
float newHeight = 200.0;
CGRect textViewFrame = textMultiline1.frame;
textMultiline1.frame = CGRectMake(textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, newHeight);
Setting the content size will only change the inner TextView area, resulting in a longer (or shorter, depending on the values you set) scroll

UITabBar setSelectionIndicatorImage in iOS 4

i want know how i can use an image to the selected item in a UITabBar, in ios5 i'll do in this way:
[[myTabBar tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"TabBar_Activated.png"]];
but in iOS 4 that line give me an error, how i can do?
Try with this..
This will show the image when you select the tab...
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selection-tab.png"]];
And this will set the image for tab bar...
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Me" image:[UIImage imageNamed:#"tab-me.png"] tag:1];
//Set this tabitem in current view's tabBar.
[self setTabBarItem:tab1];

Resources