i have a VC with a UITextField. Thats my Code:
.h
#interface GameOverViewController : UIViewController <UITextFieldDelegate> {
}
.m
#implementation UITextField (DisableCopyPaste)
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
return [super canPerformAction:action withSender:sender];
}
#end
#implementation GameOverViewController
NSString *text1;
text1 = #"";
UIView* testView = [[UIView alloc] init];
_nameField.inputView = testView;
[_nameField becomeFirstResponder];
_nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
This is for hiding the Keyboard:
UIView* testView = [[UIView alloc] init];
_nameField.inputView = testView;
But now there is still the TopBar of the Keyboard on my View:
If i take out this two lines of code, the whole Keyboard appears:
i can't get it hided. i don't want this bar.
if i disable the UITextField, the Bar is gone.
Found the solution:
in iOS 9 there is an UITextInoutAssistantItem, and here is my Solution for this Bar:
if ([self respondsToSelector:#selector(inputAssistantItem)]) {
UITextInputAssistantItem *item = [self inputAssistantItem];
item.leadingBarButtonGroups = #[];
item.trailingBarButtonGroups = #[];
}
Related
I have the problem like this: Hide UISearchBar Cancel Button
I have subclassed UISearchController and UISearchBar, and override methods layoutSubviews in UISearchBar.
How to override searchBar property with custom UISearchBar in my custom UISearchController?
Update 1
To implement property with custom UISearchBar i do in MyUISearchController:
#property(nonatomic, strong, readonly) UISearchBar *searchBar;
And
#synthesize searchBar=_searchBar;
- (UISearchBar *)searchBar {
return [MySearchBar new];
}
But UISearchController uses default searchBar anyway..
Update
In h file:
#interface MySearchController : UISearchController <UITableViewDelegate, UITableViewDataSource>
In m file:
#interface MySearchController () <UISearchDisplayDelegate, UISearchControllerDelegate,
UISearchBarDelegate, UISearchResultsUpdating, SearchAutocompleteLoader>
#property UISearchController *searchController;
#property (strong, nonatomic) UITableView *searchResultTable;
#property UIView *viewForSearchBar;
#property (nonatomic) NSInteger filterSettings;
#property (strong, nonatomic) UILabel *matchesLabel;
#property (strong, nonatomic) UIView *loading;
#property (strong, nonatomic) UIView *foggView;
#end
- (instancetype)initWith:(UIView *)viewForSearch foggView:(UIView *)view delegate:(id)delegate andResultTableView:(UITableView *)tableView
{
self.viewForSearchBar = viewForSearch;
self.searchResultTable = tableView;
self.foggView = view;
self.searchDelegate = delegate;
[self configureSearchController];
return self;
}
- (void)configureSearchController {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
[self loadFilterSettings];
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar setShowsCancelButton:NO];
//self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
[self.searchController.searchBar setBackgroundImage:[UIImage imageWithCGImage:(__bridge CGImageRef)([UIColor clearColor])]];
[self.searchController.searchBar setImage:[UIImage imageNamed:#"iconSearchSettings"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, self.viewForSearchBar.frame.size.height);
self.viewForSearchBar.tintColor = [UIColor yellowColor];
// [self.viewForSearchBar addSubview:self.searchController.searchBar];
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
self.searchController.searchBar.tintColor = [UIColor blackColor];
//self.searchController.searchBar.backgroundColor = [UIColor whiteColor];
[self.viewForSearchBar addSubview:self.searchController.searchBar];
for (UIView *subView in self.searchController.searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
[subView setTintColor:[UIColor blueColor]];
[[(UITextField *) subView valueForKey:#"textInputTraits"] setValue:[UIColor blueColor] forKey:#"insertionPointColor"];
}
}
[self.searchController setActive:YES];
[self.searchController setActive:NO];
}
UISearchController's searchBar is a computed property, you have to store your custom searchBar and return it when call searchBar.
Here is some code in Swift:
var mySearchBar = MySearchBar()
override var searchBar: UISearchBar {
get{
return mySearchBar;
}
}
In order to customize the searchBar property begin the customization process by subclassing first the search bar (UISearchBar). Then, use this custom search bar in the subclass of the search controller, and at the end use both of them in the your ViewController class.
Refer this tutorial:
UISearchController Customization
import UIKit
class MySearchController: UISearchController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
searchBar.showsCancelButton = false
}
}
See this one tutorial for customise the UISearchBar in Swift:
Customise UISearchBar
Objective - C : Add the code in ViewDidLoad. You have already declare object of UISearchBar
[searchBar setShowsCancelButton:NO];
Swift : in ViewDidLoad
searchBar.showsCancelButton = false
I'm wondering if there is a way to slide the first-responder keyboard to the left or to the right when the user press a button.
http://cl.ly/image/143K3t403d1m/1.png
I make the button as a keyboard-accessory view. When it's tapped the keyboard should slide to the left displaying another custom inputs panel.
Any ideas?
If you want a keyboard that can be selected from the system (using the globe key), you'll need to read this article about creating a Custom Keyboard App Extension for iOS 8.
If you just want to be able to switch between inputView objects, the following code will get you there:
// ViewController.m
#import "ViewController.h"
#interface ViewController () {
}
#pragma mark -
#pragma mark - UI Controls
#property (strong, nonatomic) UIInputView *inputView;
#property (strong, nonatomic) UITextField *textField;
#end
#implementation ViewController
#pragma mark -
#pragma mark - View Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUserInterface];
}
#pragma mark -
#pragma mark - Keyboard switching
- (void)switchKeyboard {
// Simply toggle the "inputView" for self.textField
if (self.textField.inputView == nil) {
self.textField.inputView = self.inputView;
} else {
self.textField.inputView = nil;
}
[self.textField resignFirstResponder];
[self.textField becomeFirstResponder];
}
#pragma mark -
#pragma mark - UI Setup
// All of the code below here
// is for pure, in-code AutoLayout
- (void)setupUserInterface {
[self createControls];
[self setupControls];
[self layoutControls];
}
- (void)createControls {
self.textField = [[UITextField alloc] init];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.inputView = [[UIInputView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 216.0f)];
self.inputView.backgroundColor = [UIColor lightGrayColor];
}
- (void)setupControls {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44.0f)];
toolbar.backgroundColor = [UIColor lightGrayColor];
// When the button is tapped, it'll execute "switchKeyboard" above
UIBarButtonItem *switchButton = [[UIBarButtonItem alloc] initWithTitle:#"Switch" style:UIBarButtonItemStyleDone target:self action:#selector(switchKeyboard)];
toolbar.items = #[switchButton];
self.textField.inputAccessoryView = toolbar;
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
}
- (void)layoutControls {
[self.view addSubview:self.textField];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[textfield]-|"
options:0
metrics:nil
views:#{#"textfield": self.textField}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(height)-[textfield(==height)]"
options:0
metrics:#{#"height" : #(40)}
views:#{#"textfield": self.textField}]];
}
#end
Caveat: You will need to do a lot of tweaking to make sure the user experience is good. In my testing, on the simulator (iPhone 4S), this works ok, but isn't the smoothest user experience.
I am trying to change self.navigationController.toolbar position to top instead of a bottom.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIToolbarDelegate, UIBarPositioningDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
// View did load
- (void)viewDidLoad
{
// Superclass view did load method call
[super viewDidLoad];
// UIToolbarDelegate
self.navigationController.toolbar.delegate = self;
}
- (void)viewDidLayoutSubviews
{
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.frame = CGRectMake(self.navigationController.toolbar.frame.origin.x, 64.0f, self.navigationController.toolbar.frame.size.width, self.navigationController.toolbar.frame.size.height);
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"First", #"Second"]];
segmentedControl.frame = CGRectMake(50.0f, 10.0f, 220.0f, 24.0f);
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.toolbar addSubview:segmentedControl];
}
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
#end
The position changes at first, but later gets back to the bottom. Please, help me!
First make sure that toolbar delegate method positionForBar: is get called. If not, then your toolbar delegate is not properly set and you've have to define the UIToolbarDelegate protocol inside your header class.
#interface ExampleViewController : UIViewController <UIToolbarDelegate>
...
#end
Then set the delegate and also change the frames for toolbar. However, for the orientation you might have to reconsider the frames of toolbar.
#implementation ExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
// set the toolbar delegate
self.navigationController.toolbar.delegate = self;
self.navigationController.toolbarHidden = NO;
// create a toolbar child items
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"First", #"Second"]];
segmentedControl.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.with, 24.0f);
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.toolbar addSubview:segmentedControl];
// set the frames
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y = self.navigationController.frame.size.height;
self.navigationController.toolbar.frame = toolbarFrame;
}
// pragma mark - UIToolbarDelegate
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
#end
In your viewDidLoad method, add:
self.navigationController.toolbar.delegate = self;
I'm trying to create a class that makes a read only text field that allows the user to copy its contents. Here is my code:
CopyOnly.h
#import <UIKit/UIKit.h>
#interface CopyOnly : UITextField
#end
CopyOnly.m
#import "CopyOnly.h"
#implementation CopyOnly
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self attachTapHandler];
}
return self;
}
- (void) attachTapHandler
{
[self setUserInteractionEnabled:YES];
UIGestureRecognizer *touchy = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self addGestureRecognizer:touchy];
}
- (BOOL) canPerformAction: (SEL) action withSender: (id) sender
{
return (action == #selector(copy:));
}
- (void) handleTap: (UIGestureRecognizer*) recognizer
{
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
- (void)copy:(id)sender
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
#end
This works great, only the keyboard is coming up. I don't want any keyboard to come up.
I tried adding this to initWithFrame:
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
This does not give me the result I'm expecting. Anyone know how I can do this?
Extending on my comment. This is easily accomplished using a UITextView (not UITextField) with editable property set to NO.
UITextView* tf = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
tf.editable = NO;
tf.text = #"Hey this is a test!";
[self.view addSubview:tf];
Adding this to -(BOOL)canBecomeFirtResponder seemed to do the trick. Hacky, but it works.
- (BOOL) canBecomeFirstResponder
{
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
return YES;
}
If you stuck with text field that doesn't allow editing try totally different approach. Follow instructions in this article http://nshipster.com/uimenucontroller/ to implement UILabel that supports copying.
I currently have a table view controller and have placed a UISearchDisplayController on top or above the my table view controller inside the interface builder. Inside the viewDidLoad of my implementation file I go ahead and assign the searchbar to the navigationItem.titleView. The problem that I now have is that there is a bit more space in the first table view cell because it still thinks that the searchdisplaycontroller is there. You can see it here: http://imgur.com/u6cQew2
Here is code:
.h file
#interface searchTableViewController : UITableViewController <UISearchBarDelegate>
{
UISearchBar *searchDrugBar;
}
#property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
searchDrugBar.placeholder = #"Search for info";
self.navigationItem.titleView = self.searchDisplayController.searchBar;
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}
Any suggestions or ideas? Thanks in advance!
Try to create the searchBar programatically, I don't see any added value from creating it in the storyboard unless you have your own reason.
First remove it from the storyboard then:
- (void)viewDidLoad
{
[super viewDidLoad];
searchDrugBar = [[UISearchBar alloc] init];
searchDrugBar.frame = CGRectMake(0, 0, 200,44); // it could be unnecessary
searchDrugBar.delegate = self;
searchDrugBar.placeholder = #"Search for info";
self.navigationItem.titleView = self.searchDisplayController.searchBar;
[self.searchDisplayController setActive: YES animated: YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}
self.navigationItem.titleView = self.searchBarTop;
=> to pur searchbar left/right :-
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
==> Here Given more Reference It might be Useful I hope it work for You..
How to animate add UISearchBar on top of UINavigationBar