UITexFiled always clear when I hit return - objective-c

I add a UITexfield programatically on my view. When I use keyboard and hit return button texfield cleared automatically and I don't want this.
Here is my code;
UITextField * eventListSeractTextField = [[UITextField alloc]initWithFrame:CGRectMake(headerView.frame.size.width-112,4, 110, 20)];
eventListSeractTextField.backgroundColor=[UIColor whiteColor];
eventListSeractTextField.layer.cornerRadius=3.0f;
eventListSeractTextField.tag=101;
eventListSeractTextField.delegate=self;
eventListSeractTextField.autocapitalizationType=UITextAutocapitalizationTypeNone;
eventListSeractTextField.autocorrectionType=UITextAutocorrectionTypeNo;
eventListSeractTextField.spellCheckingType=UITextSpellCheckingTypeNo;
eventListSeractTextField.clearButtonMode = UITextFieldViewModeAlways;
eventListSeractTextField.returnKeyType = UIReturnKeySearch;
eventListSeractTextField.placeholder=#"Search";
eventListSeractTextField.clearsOnBeginEditing=false;
[eventListSeractTextField setClearsOnBeginEditing:false];
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
switch (textField.tag) {
case 101:
[textField resignFirstResponder];
return false;
break;
default:
return YES;
}
return YES;
}
How can I avoid this?

My guess is that you are recreating the textview each time since you did it in code. Check where you are calling this line of code:
UITextField * eventListSeractTextField = [[UITextField alloc]initWithFrame:CGRectMake(headerView.frame.size.width-112,4, 110, 20)];
You should only need to create the textView once in viewDidLoad. The easier way would be to do it in storyboard if you are familiar with storyboard

Related

How can I add a UISwitch to a specific UIScrollView?

how can I add a UISwitch to a specific UIScrollView? As I guess it can be done with a tag, but I am having difficulties.
EDIT: The problem was solved in the usual way, lol
My example code with UIScrollView and UISwitch:
#implementation NSPage {
UIScrollView *scrolled;
UISwitch *switchPage;
NSInteger *keyS;
}
NSPage *page = [[NSPage alloc]init];
Here i am creating a UIScrollView and UISwitch:
-(id)initPage:(NSString *)titleScroll keyScroll:(NSInteger)keyScroll_ {
keyS = keyScroll_;
scrolled = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 180, 110)];
scrolled.tag = keyS;
scrolled.backgroundColor = [UIColor grayColor];
[window addSubview:scrolled];
}
//get scroll tag
-(NSInteger)getScrollKey {
return scrolled.tag = keyS;
}
//UISwitch
-(id)initSwitch:(NSString *)title_ keySwitch:(NSInteger)keySwitch_ {
scrollSwitch += 40;
switch_ = [[UISwitch alloc]initWithFrame:CGRectMake(15, scrollSwitch - 40, 40, 40)];
switch_.tag = keySwitch_;
if([self getScrollKey] == switch_.tag) {
[scrolled addSubview:switch_];
}
}
#end
Everything seemed to be fine, but when I add a scrolls and switches, then my UISwitch is not displayed:
page = [[NSPage alloc]initPage:#"test page" keyScroll:256];
page = [[NSPage alloc]initSwitch:#"test" keySwitch:256];
What could be my mistake, how can I add a UISwitch to a specific UIScrollView? I'm a beginner, Thanks for the help anyway
if([self getScrollKey] == switch_.tag) {
This line does nothing take it out

regarding scrolling inside UIScrollView

I have a scroll view with some content. After loading the contents I am using the following code to move to the top of the scroll view
[scrollViewCustom setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
It works. With one exception :
I have a UITextView. If I add some text to it, then the scroll view never goes back to the top with this code. I have to manually scroll to the top.
I have even tried to put the same code after adding text to the UITextView.
Can anyone kindly help out ? Thanks.
NB:So, heres the solution :
Basically UITextView derives from UIScrollView and as such, whenever there is some text there, it tries to scroll to make the text visible. I have replaced UITextView to UILabel and it works like a charm !
Instead of
[scrollViewCustom setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
Use
[scroll scrollRectToVisible:CGRectMake(0,0, 100,100) animated:NO];
- (void)viewDidLoad{
scroll.contentSize=CGSizeMake(320, 400);
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight;
availableHeight = applicationFrame.size.height - 300;
CGFloat z = viewCenterY - availableHeight / 2.0;
if (z < 0) {
z = 0;
}
[scroll setContentOffset:CGPointMake(0, z) animated:YES];
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
[self scrollViewToCenterOfScreen:scroll];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.length==0) {
[scroll setContentOffset:CGPointMake(0, 0) animated:YES];
return NO;
}
}
Just two steps:
// define the area that is initially visible
scrollViewCustom.frame = CGRectMake(0, 5, 354, 500);
// then define how much a user can scroll it
[scrollViewCustom setContentSize:CGSizeMake(354, 960)];
In addition,clear the background color of scroll view to show the text.
scrollViewCustom.backgroundColor = [UIColor clearColor];

Custom navbar titleView won't clear previous titles before loading new ones

So I created a custom UINavigationItem category to be able to make a custom titleview for my navbar, but everytime I push/pop a view, it simply adds the new title without getting rid of the old one causing the title to just be a jumble of letters. Here's the relevant code:
#implementation UINavigationItem (CustomNavigationItem)
-(UIView *)titleView
{
[self setTitleView:nil];
UILabel *newTitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 32)];
newTitleView.center = CGPointMake(160, 22);
newTitleView.backgroundColor = [UIColor clearColor];
newTitleView.textColor = [UIColor whiteColor];
newTitleView.textAlignment = UITextAlignmentCenter;
newTitleView.text = self.title;
newTitleView.textAlignment = UITextAlignmentCenter;
return newTitleView;
}
#end
You have to remove the old uilabel from its superview, by setting to nil it doesn't do that. That's why you are messing the letters on screen. I also do not think you are getting a recursion, because you are caling the setter, but I maybe wrong.
A quick thing you could is to assign a tag to your newest created view.
[[self.view viewWithTag:YourCustomEnumTag] removeFromSuperView];
// create your view....
textView.tag=YourEnumCustomTag;

UITextField rightViewMode odd behaviour

I'm adding a custom clear button (UIButton) to a UITextField as the rightView, however I've found there's some weird behaviour on the viewMode. It doesn't seem to display as the normal clear button does, despite the view mode being set. Example code below:
UITextField *f = [[[UITextField alloc] init] autorelease];
f.frame = CGRectMake(0, 0, 300, 44);
f.backgroundColor = [UIColor clearColor];
f.textColor = [UIColor whiteColor];
f.clearButtonMode = UITextFieldViewModeNever;
UIImage *image = [UIImage imageNamed:#"Image.png"];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[b setImage:image forState:UIControlStateNormal];
f.rightView = b;
f.rightViewMode = UITextFieldViewModeWhileEditing;
The button displays correctly in the following states:
Shows while focused and no text
Shows while focused and typing
Hides when no focus
However, if the textfield already has content, and you switch focus to it the clear button does not show. To get it to show again you must delete all text, and switch focus back and forth.
I haven't found anyone else with this problem, so have been scratching my head on this one for a while. Any light shedding very much appreciated.
This fixes the bug :
- (BOOL)becomeFirstResponder
{
BOOL ret = YES ;
ret = [super becomeFirstResponder] ;
if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
self.rightViewMode = UITextFieldViewModeAlways ;
return ret ;
}
- (BOOL)resignFirstResponder
{
BOOL ret = YES ;
ret = [super resignFirstResponder] ;
if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
self.rightViewMode = UITextFieldViewModeWhileEditing ;
return ret ;
}
In your subclass of UITextField
with the var _setupClearButtonMode set on init.
I recently ran into the same problem and ended up setting right view mode to UITextFieldViewModeAlways and manually showing/hiding that button when it's needed (made proxy delegate which monitored text field state, set button's visibility and passed messages to actual delegate).
Simple code for solve this problem
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.rightViewMode=UITextFieldViewModeAlways;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.rightViewMode=UITextFieldViewModeNever;
return YES;
}

Sample code for creating a NSTextField "label"?

In my desktop Mac OS X app, I'd like to programatically create a NSTextField "label" which has the same behavior and properties as a typical label created in Interface Builder.
I usually use (and very much like) IB, but in this case it must be done programatically.
Try as I might, I can't seem to find the combination of method calls that will programatically produce the same label-y behavior as a "Label" dragged from the IB View Library palette.
Can anyone provide or point out some example code of how to do this programatically? Thx.
A label is actually an instance of NSTextField, a subclass of NSView. So, since it is a NSView, it has to be added to another view.
Here's a working code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSTextField *textField;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 200, 17)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[view addSubview:textField];
}
macOS 10.12 and Later
Starting with macOS 10.12 (Sierra), there are three new NSTextField constructors:
NSTextField(labelWithString:), which the header file comment says “Creates a non-wrapping, non-editable, non-selectable text field that displays text in the default system font.”
NSTextField(wrappingLabelWithString:), which the header file comment says “Creates a wrapping, non-editable, selectable text field that displays text in the default system font.”
NSTextField(labelWithAttributedString:), which the header file comment says “Creates a non-editable, non-selectable text field that displays attributed text. The line break mode of this field is determined by the attributed string's NSParagraphStyle attribute.”
I tested the ones that take a plain (non-attributed string), and they create text fields that are similar to, but not precisely the same as, the text fields created in a storyboard or xib.
The important difference is that both constructors create a text field with textBackgroundColor (normally pure white) as its background color, while the storyboard text field uses controlColor (normally about 90% white).
Unimportantly, both constructors also set their fonts by calling NSFont.systemFont(ofSize: 0) (which produces a different NSFont object than my code below, but they wrap the same underlying Core Text font).
The wrappingLabelWithString: constructor sets the field's isSelectable to true. (This is documented in the header file.)
macOS 10.11 and Earlier
I compared four NSTextField instances: one created by dragging a “Label” to a storyboard, another created by dragging a “Wrapping Label” to a storyboard, and two in code. Then I carefully modified properties of the code-created labels until all their properties were exactly the same as the storyboard-created labels. These two methods are the result:
extension NSTextField {
/// Return an `NSTextField` configured exactly like one created by dragging a “Label” into a storyboard.
class func newLabel() -> NSTextField {
let label = NSTextField()
label.isEditable = false
label.isSelectable = false
label.textColor = .labelColor
label.backgroundColor = .controlColor
label.drawsBackground = false
label.isBezeled = false
label.alignment = .natural
label.font = NSFont.systemFont(ofSize: NSFont.systemFontSize(for: label.controlSize))
label.lineBreakMode = .byClipping
label.cell?.isScrollable = true
label.cell?.wraps = false
return label
}
/// Return an `NSTextField` configured exactly like one created by dragging a “Wrapping Label” into a storyboard.
class func newWrappingLabel() -> NSTextField {
let label = newLabel()
label.lineBreakMode = .byWordWrapping
label.cell?.isScrollable = false
label.cell?.wraps = true
return label
}
}
If you use one of these methods, don't forget to set your field's frame, or turn off its translatesAutoresizingMaskIntoConstraints and add constraints.
Here is the code I used to compare the different text fields, in case you want to check:
import Cocoa
class ViewController: NSViewController {
#IBOutlet var label: NSTextField!
#IBOutlet var multilineLabel: NSTextField!
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
let codeLabel = NSTextField.newLabel()
let codeMultilineLabel = NSTextField.newWrappingLabel()
let labels = [label!, codeLabel, multilineLabel!, codeMultilineLabel]
for keyPath in [
"editable",
"selectable",
"allowsEditingTextAttributes",
"importsGraphics",
"textColor",
"preferredMaxLayoutWidth",
"backgroundColor",
"drawsBackground",
"bezeled",
"bezelStyle",
"bordered",
"enabled",
"alignment",
"font",
"lineBreakMode",
"usesSingleLineMode",
"formatter",
"baseWritingDirection",
"allowsExpansionToolTips",
"controlSize",
"highlighted",
"continuous",
"cell.opaque",
"cell.controlTint",
"cell.backgroundStyle",
"cell.interiorBackgroundStyle",
"cell.scrollable",
"cell.truncatesLastVisibleLine",
"cell.wraps",
"cell.userInterfaceLayoutDirection"
] {
Swift.print(keyPath + " " + labels.map({ ($0.value(forKeyPath: keyPath) as? NSObject)?.description ?? "nil" }).joined(separator: " "))
}
}
}
This can be tricky to get right. I don't have the recipe for an exact replica handy, but when I've been stuck in a similar situation, here's what I do:
Create a UI element in IB.
Add an outlet to it from my controller class.
Break in gdb in awakeFromNib or whatever.
From the gdb prompt, "p *whateverOutlet" ... this will show you the C struct contents of the label NSTextField that IB set up.
By looking at all the myriad values in there, you can get a lot of guesses about what you're neglecting to set. Usually it ends up being some magic combination of bezel and border settings, that gets you where you want to be.
You could try using nib2objc to get all the properties that IB sets
Disassembled AppKit in Objective-C:
BOOL TMPSierraOrLater() {
static BOOL result = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
result = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 10, 12, 0 }];
});
return result;
}
#implementation NSTextField (TMP)
+ (instancetype)TMP_labelWithString:(NSString *)stringValue {
if (TMPSierraOrLater()) {
return [self labelWithString:stringValue];
}
NSParameterAssert(stringValue);
NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
label.lineBreakMode = NSLineBreakByClipping;
label.selectable = NO;
[label setContentHuggingPriority:(NSLayoutPriorityDefaultLow + 1) forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
label.stringValue = stringValue;
[label sizeToFit];
return label;
}
+ (instancetype)TMP_wrappingLabelWithString:(NSString *)stringValue {
if (TMPSierraOrLater()) {
return [self wrappingLabelWithString:stringValue];
}
NSParameterAssert(stringValue);
NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.selectable = YES;
[label setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
label.stringValue = stringValue;
label.preferredMaxLayoutWidth = 0;
[label sizeToFit];
return label;
}
+ (instancetype)TMP_labelWithAttributedString:(NSAttributedString *)attributedStringValue {
if (CRKSierraOrLater()) {
return [self labelWithAttributedString:attributedStringValue];
}
NSParameterAssert(attributedStringValue);
NSTextField *label = [NSTextField TMP_newBaseLabelWithoutTitle];
[label setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
[label setContentCompressionResistancePriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical];
label.attributedStringValue = attributedStringValue;
[label sizeToFit];
return label;
}
#pragma mark - Private API
+ (instancetype)TMP_newBaseLabelWithoutTitle {
NSTextField *label = [[self alloc] initWithFrame:CGRectZero];
label.textColor = NSColor.labelColor;
label.font = [NSFont systemFontOfSize:0.0];
label.alignment = NSTextAlignmentNatural;
label.baseWritingDirection = NSWritingDirectionNatural;
label.userInterfaceLayoutDirection = NSApp.userInterfaceLayoutDirection;
label.enabled = YES;
label.bezeled = NO;
label.bordered = NO;
label.drawsBackground = NO;
label.continuous = NO;
label.editable = NO;
return label;
}
#end
Specifically, you will want to setBordered:NO, and set the bezel style to whatever that bezel style is which I forgot. Also setEditable:NO, and optionally setSelectable:NO. That should suffice.