UISearchController background not matching UINavigationBar background in iOS 11 - objective-c

I'm updating an older app to support iPhone X and when updating a searchable UITableViewController the background of the UISearchController doesn't change when placing it in the navigation bar.
I'm using the following code to place it in the navigation bar:
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
Inspecting the Interface shows the following hierarchy:
_UIBarBackground contained within the _UINavigationControllerManagedSearchPalette remains white.
Is there something I've missed when creating the UINavigationController?

Try to apply background color same as the navigationBar tint color.
searchBar.tintColor = UIColor.white
searchBar.backgroundColor = UIColor.red
searchBar.clearBackgroundColor()
Remove color of the searchBar by this way:
extension UISearchBar {
func clearBackgroundColor() {
guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }
for view in self.subviews {
for subview in view.subviews {
if subview.isKind(of: UISearchBarBackground) {
subview.alpha = 0
}
}
}
}
}
with above code I am able to achieve this output
Hope this help.

Related

Sending uitextview data to container view

I have a view controller with a container view embedded that contains a uibutton and a uilabel. In the view controller, I have a uitextview with it's character range limited to 150.
What I am trying to do is get the uibutton located in the container view to change it's state when text is entered in the uitextview and also get the uilabel (character counter) to decrease as text is entered.
I'm just looking for a shove in the right direction. I would like to post an image for clarity, but sadly I cannot.
You can extend your VC which have your container and textField with UITextViewDelegate. After that you will you use "textViewDidEndEditing" method to do your jobs. For example:
extension myVC: UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
myButton.isHidden = true
}
}
If you have more than one textView and do sth different, you need to set tag to textView's.
myTextView.tag = 1
extension myVC: UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
if textView.tag == 1 {
myButton.isHidden = true
}
}
}
I hope I understood very well your question and help you.

Hide UITabBar in UITabBarController with autolayout

I'm trying to hide the tab bar from within a UITabBarController.
This successfully hides the tab bar:
self.tabBar.hidden = true
However, I now have a black "blank spot" where the tab bar used to be.
I've seen some solutions to this problem on SO, but they modified the frames directly, and didn't take into autolayout.
How can I stretch the main view to fill the rest of the screen, with autolayout?
Often times its because, the controllers view is not allowed to extend under the bottom bar. You can enable this by
self.edgesForExtendedLayout = UIRectEdgeBottom;
The viewcontroller in which you wish to hide the tabbar should override the variable:
override var hidesBottomBarWhenPushed: Bool {
get {
return navigationController?.topViewController == self
}
set {
super.hidesBottomBarWhenPushed = newValue
}
}
It will take care of your bottom autolayouts too and adjusts the empty space in bottom as well.

How to implement the behaviour seen in the iOS lock screen when touching the camera icon?

I'm trying to implement the behaviour seen in the iOS lock screen when touching the camera icon, using a UIPageViewController
I have a UIPageViewController that vertically scrolls between 2 view controllers. I would like to do a small "bounce/bump" when the user taps the view.
Any ideas on how I can accomplish this?
Thank you!!
Thanks to #Szu for pointing me out to UIKit Dynamics :)
Here's a rough solution:
Call init with a controller's view (to be bounced) and a reference view (your current view controller's view property).
eg.
FSBounceAnimator(contentView: viewController.view, referenceView: self.view)
viewController is any viewController that you may instantiated and added as a childViewController
self.view is your current viewController view.
Call bounce(), to bounce :)
import UIKit
class FSBounceAnimator: NSObject {
var animator: UIDynamicAnimator
var gravityBehaviour: UIGravityBehavior
var pushBehavior: UIPushBehavior
var itemBehaviour: UIDynamicItemBehavior
init(contentView: UIView!, referenceView: UIView!) {
animator = UIDynamicAnimator(referenceView: referenceView)
var colisionBehaviour: UICollisionBehavior = UICollisionBehavior(items: [contentView])
colisionBehaviour.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsetsMake(-100, 0, 0, 0))
animator.addBehavior(colisionBehaviour)
gravityBehaviour = UIGravityBehavior(items: [contentView])
gravityBehaviour.gravityDirection = CGVectorMake(1, 1)
animator.addBehavior(gravityBehaviour)
pushBehavior = UIPushBehavior(items: [contentView], mode: UIPushBehaviorMode.Instantaneous)
pushBehavior.magnitude = 0.0
pushBehavior.angle = 0.0
animator.addBehavior(pushBehavior)
itemBehaviour = UIDynamicItemBehavior(items: [contentView])
itemBehaviour.elasticity = 0.45
animator.addBehavior(itemBehaviour)
super.init()
}
func bounce() {
self.pushBehavior.pushDirection = CGVectorMake(0.0, 100.0);
self.pushBehavior.active = true;
}
}

Cursor invisible in UISearchBar iOS 7

I have UISearchBar in UITableView as a table header. When I push the UISearchBar for start searching, this method is being triggered
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
for UISearchDisplayController.
But result is like that;
As you can see, there is no cursor, I can start typing and search, everything works fine. Also it's invisible only in iOS 7. However, with iOS 6.1 and iOS 7.1 Beta 3 I could see the cursor. So how can I make UISearchBar cursor visible or how can I add cursor in my UISearchBar?
Cursor in the search bar takes color from Search Bar -> View -> Tint Color property.
In your case, it is set to White color, so it becomes invisible.
try using with
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
self.navigationItem.titleView.tintColor = [UIColor blueColor];
}
hope this will help you
Try setting text field tint color using UIAppearance
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor: [UIColor darkGrayColor]];
If you want the cursor and cancel button to be different colors...
Start by setting the view's tint color (not the bar tint) in the storyboard editor, which will be applied to both the cursor and cancel button.
To make the cursor a different color, you need to do it programatically. The text field is nested a couple levels down in the searchBar's subviews. Use this setTextFieldTintColor helper function to traverse all of the subviews.
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
// set tint color for all subviews in searchBar that are of type UITextField
setTextFieldTintColor(to: UIColor.darkText, for: searchBar)
}
func setTextFieldTintColor(to color: UIColor, for view: UIView) {
if view is UITextField {
view.tintColor = color
}
for subview in view.subviews {
setTextFieldTintColor(to: color, for: subview)
}
}
The end result looks like this:
Try this it's only one line of code to solve your problem , Change cursor tintColor property white to blue.
searchBar.tintColor = [UIColor blueColor];
Hope this will help to someone .
Your cursor is white because your tintColor property on UISearchBar is set to white.
If you want Cancel btn to be white, but cursor to be black you can use:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black

Change link color in UITextView in iphone [duplicate]

I had a UITextView that detects phone numbers and links, but this overrides my fontColor and change it to blueColor. Is there a way to format the color of auto detected links, or should I try a manual version of this function?
On iOS 7 you can set the tintColor of the UITextView. It affects the link color as well as the cursor line and the selected text color.
iOS 7 also added a new property to UITextView called linkTextAttributes which would appear to let you fully control the link style.
You can use the UIAppearance protocol to apply changes for all text views:
Swift 4.x+:
UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]
Swift 3.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.red ]
Swift 2.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.redColor() ]
Objective-C:
[UITextView appearance].linkTextAttributes = #{ NSForegroundColorAttributeName : UIColor.redColor };
Appearance for UITextView is not documented, but works well.
Keep in mind UIAppearance notes:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
In other words:
Calling this code in init(), or init(coder:) methods will change UI Object appearance, but calling in loadView(), or viewDidLoad() of viewController won't.
If you want to set appearance for whole application, application(_:didFinishLaunchingWithOptions:) is good place for calling such code.
Instead of using an UITextView, I used an UIWebView and enabled the "auto-detect links". To change the link color, just created a regular CSS for the tag.
I used something like this:
NSString * htmlString = [NSString stringWithFormat:#"<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0; } p { color:black; font-family:Helvetica; font-size:14px; } a { color:#63B604; text-decoration:none; }</style></head><body><p>%#</p></body></html>", [update objectForKey:#"text"]];
webText.delegate = self;
[webText loadHTMLString:htmlString baseURL:nil];
The problem with UITextView linkTextAttributes is that it applies to all automatically detected links. What if you want different links to have different attributes?
It turns out there's a trick: configure the links as part of the text view's attributed text, and set the linkTextAttributes to an empty dictionary.
Here's an example in iOS 11 / Swift 4:
// mas is the mutable attributed string we are forming...
// ... and we're going to use as the text view's `attributedText`
mas.append(NSAttributedString(string: "LINK", attributes: [
NSAttributedStringKey.link : URL(string: "https://www.apple.com")!,
NSAttributedStringKey.foregroundColor : UIColor.green,
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]))
// ...
self.tv.attributedText = mas
// this is the important thing:
self.tv.linkTextAttributes = [:]
You can Change the Hyperlink Color in a TextView by the following:
In the Nib file, you can go to the Properties Window and change the Tint to which ever color you want to.
or you can also do it programatically by using the below code
[YOURTEXTVIEW setTintColor:[UIColor whiteColor]];
Swift 5 Answer
Nice and simple
myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
The tint color can be done in the storyboard also
I found indeed another way without using a webview but keep in mind that this uses private API and may be rejected in appstore:
EDIT: My app got approved by apple although the private api usage!
First declare a category on UITextView with the methods
- (id)contentAsHTMLString;
- (void)setContentToHTMLString:(id)arg1;
They are just doing the following:
- (id)contentAsHTMLString;
{
return [super contentAsHTMLString];
}
- (void)setContentToHTMLString:(id)arg1;
{
[super setContentToHTMLString:arg1];
}
Now write a method for colorful links:
- (void) colorfillLinks;
{
NSString *contentString = [self.textViewCustomText contentAsHTMLString];
contentString = [contentString stringByReplacingOccurrencesOfString:#"x-apple-data-detectors=\"true\""
withString:#"x-apple-data-detectors=\"true\" style=\"color:white;\""];
[self.textViewCustomText setContentToHTMLString:contentString];
}
It does set the style attribute with a specific color on all types of links.
UITextViews are rendered Webiview like via divs so you could even go further and color each link type separately:
<div>http://www.apple.com</div>
The x-apple-data-detectors-type="link" is the indicator for the exact type of the link
EDIT
On iOS7this is no longer working. In iOS7 you could easily change the link color of UITextViews by setting the tint color. You should not call
- (id)contentAsHTMLString;
anymore, you'll get an exception. Instead do the following if you want to support iOS 7 and below:
- (void) colorfillLinks;
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.tintColor = [UIColor colorWithRed:79.0/255.0
green:168.0/255.0
blue:224.0/255.0
alpha:1.0];
} else if(![self isFirstResponder ]) {
NSString *contentString = [self contentAsHTMLString];
contentString = [contentString stringByReplacingOccurrencesOfString:#"x-apple-data-detectors=\"true\""
withString:#"x-apple-data-detectors=\"true\" style=\"color:#DDDDDE;\""];
[self setContentToHTMLString:contentString];
}
}
EDIT:
Don't do it with UITextView, use UIWebView instead.
You need to make a stylesheet for that. Define a class there with the color combination you need-
.headercopy {
font-family: "Helvetica";
font-size: 14px;
line-height: 18px;
font-weight:bold;
color: #25526e;
}
a.headercopy:link {
color:#ffffff;
text-decoration:none;
}
a.headercopy:hover {
color:#00759B;
text-decoration:none;
}
a.headercopy:visited {
color:#ffffff;
text-decoration:none;
}
a.headercopy:hover {
color:#00759B;
text-decoration:none;
}
now use the class 'headercopy' into you html page like this-
<b>Fax:</b> 646-200-7535<br />
this will display the phone number in the color you need with click functionality.
This code will set the colour of a phone number on an I-Phone but voids the automatic call link.
<div>p 0232 963 959</div>
This is how I did it using Swift 5:
let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.whiteColor] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString