Questions tagged [cocoa-touch]
The Cocoa Touch Frameworks that drive iOS apps share many proven patterns found on the Mac, but were built with a special focus on touch-based interfaces and optimization.
cocoa-touch
34,516
questions
2340
votes
51
answers
768k
views
Vertically align text to top within a UILabel
I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label.
How do I vertically align the text to always be ...
1408
votes
39
answers
514k
views
How can I check for an active Internet connection on iOS or macOS?
I would like to check to see if I have an Internet connection on iOS using the Cocoa Touch libraries or on macOS using the Cocoa libraries.
I came up with a way to do this using an NSURL. The way I ...
1324
votes
27
answers
486k
views
How do I sort an NSMutableArray with custom objects in it?
What I want to do seems pretty simple, but I can't find any answers on the web. I have an NSMutableArray of objects, and let's say they are 'Person' objects. I want to sort the NSMutableArray by ...
1323
votes
82
answers
1.0m
views
Xcode - How to fix 'NSUnknownKeyException', Reason: "… this class is not key value coding-compliant for the key X" error?
I'm trying to link a UILabel with an IBOutlet created in my class.
My application is crashing with the following error"
***
Terminating app due to uncaught exception
'NSUnknownKeyException', ...
1277
votes
42
answers
543k
views
How can I disable the UITableView selection?
When you tap a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?
819
votes
33
answers
298k
views
Loaded nib but the 'view' outlet was not set
I added a new nib file to my project, and tried to load it.
However, when I click on the toolbar icon that is supposed to take me to the view that I created, I get an NSInternalInconsistencyException ...
792
votes
32
answers
241k
views
Can I embed a custom font in an iPhone application?
I would like to have an app include a custom font for rendering text, load it, and then use it with standard UIKit elements like UILabel. Is this possible?
771
votes
34
answers
200k
views
Eliminate extra separators below UITableView
When I set up a table view with 4 rows, there are still extra separators lines (or extra blank cells) below the filled rows.
How would I remove these cells?
754
votes
63
answers
392k
views
Placeholder in UITextView
My application uses an UITextView. Now I want the UITextView to have a placeholder similar to the one you can set for an UITextField.
How to do this?
718
votes
16
answers
172k
views
@class vs. #import
It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular ...
672
votes
38
answers
442k
views
Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
Just started using Xcode 4.5 and I got this error in the console:
Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewController: 0x1ec3e000> whose view is not in the ...
659
votes
9
answers
179k
views
What does the NS prefix mean?
Many classes in Cocoa/Cocoa Touch have the NS prefix. What does it mean?
631
votes
14
answers
230k
views
Cocoa: What's the difference between the frame and the bounds?
UIView and its subclasses all have the properties frame and bounds. What's the difference?
614
votes
21
answers
399k
views
Giving UIView rounded corners
My login view has a subview which has a UIActivityView and a UILabel saying "Signing In…". This subview has corners which aren't rounded. How can I make them round?
Is there any way to do it inside ...
585
votes
51
answers
326k
views
How can I create a UIColor from a hex string?
How can I create a UIColor from a hexadecimal string format, such as #00FF00?
582
votes
19
answers
295k
views
UITableViewCell, show delete button on swipe
How do I get the delete button to show when swiping on a UITableViewCell? The event is never raised and the delete button never appears.
574
votes
11
answers
132k
views
Should IBOutlets be strong or weak under ARC?
I am developing exclusively for iOS 5 using ARC. Should IBOutlets to UIViews (and subclasses) be strong or weak?
The following:
@property (nonatomic, weak) IBOutlet UIButton *button;
Would get rid ...
572
votes
24
answers
235k
views
How to lose margin/padding in UITextView
I have a UITextView in my iOS application, which displays a large amount of text.
I am then paging this text by using the offset margin parameter of the UITextView.
My problem is that the padding of ...
572
votes
34
answers
568k
views
How do I create a basic UIButton programmatically?
How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties ...
572
votes
8
answers
165k
views
@synthesize vs @dynamic, what are the differences?
What are the differences between implementing a @property with @dynamic or @synthesize?
566
votes
41
answers
433k
views
How do I size a UITextView to its content?
Is there a good way to adjust the size of a UITextView to conform to its content? Say for instance I have a UITextView that contains one line of text:
"Hello world"
I then add another line of text:
...
564
votes
36
answers
754k
views
How would I create a UIAlertView in Swift?
I have been working to create a UIAlertView in Swift, but for some reason I can't get the statement right because I'm getting this error:
Could not find an overload for 'init' that accepts the ...
554
votes
31
answers
406k
views
How to set cornerRadius for only top-left and top-right corner of a UIView?
Is there a way to set cornerRadius for only top-left and top-right corner of a UIView?
I tried the following, but it end up not seeing the view anymore.
UIView *view = [[UIView alloc] initWithFrame:...
546
votes
18
answers
460k
views
How to get the screen width and height in iOS?
How can one get the dimensions of the screen in iOS?
Currently, I use:
lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;
in viewWillAppear: and ...
546
votes
18
answers
307k
views
How to programmatically send SMS on the iPhone?
Does anybody know if it's possible, and how, to programmatically send a SMS from the iPhone, with the official SDK / Cocoa Touch?
524
votes
14
answers
242k
views
How can I set the title of a UIButton as left-aligned?
I need to display an email address on the left side of a UIButton, but it is being positioned to the centre.
Is there any way to set the alignment to the left side of a UIButton?
This is my current ...
493
votes
38
answers
281k
views
iOS - Dismiss keyboard when touching outside of UITextField
I'm wondering how to make the keyboard disappear when the user touches outside of a UITextField.
492
votes
46
answers
306k
views
Set the maximum character length of a UITextField
How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?
458
votes
33
answers
267k
views
Getting current device language in iOS?
I'd like to show the current language that the device UI is using. What code would I use?
I want this as an NSString in fully spelled out format. (Not @"en_US")
EDIT: For those driving on by, there ...
452
votes
13
answers
147k
views
How to print out the method name and line number and conditionally disable NSLog?
I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.
In particular, I have two questions:
is there a way to easily NSLog the current ...
450
votes
36
answers
337k
views
UIView with rounded corners and drop shadow?
I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.
I want a custom UIView... : I ...
444
votes
31
answers
229k
views
Text inset for UITextField?
I would like to inset the text of a UITextField.
Is this possible?
424
votes
18
answers
400k
views
How to use background thread in swift? [closed]
How to use threading in swift?
dispatchOnMainThread:^{
NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];
418
votes
9
answers
109k
views
Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?
I want to be able to debug C structures without having to explicitly type every property that they consist of.
i.e. I want to be able to do something like this:
CGPoint cgPoint = CGPointMake(0,0);
...
415
votes
19
answers
380k
views
Set UIButton title UILabel font size programmatically
I need to set the font size of the title UILabel of a UIButton programmatically.
406
votes
21
answers
140k
views
Can you animate a height change on a UITableViewCell when selected?
I'm using a UITableView in my iPhone app, and I have a list of people that belong to a group. I would like it so that when the user clicks on a particular person (thus selecting the cell), the cell ...
394
votes
9
answers
209k
views
How do I convert NSMutableArray to NSArray?
How do I convert NSMutableArray to NSArray in objective-c?
392
votes
33
answers
209k
views
How do you add multi-line text to a UIButton?
I have the following code...
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
buttonLabel.text = @"Long text string";
[targetButton addSubview:buttonLabel];
[targetButton ...
388
votes
7
answers
97k
views
UILongPressGestureRecognizer gets called twice when pressing down
I am detecting if the user has pressed down for 2 seconds:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:...
384
votes
31
answers
213k
views
Easy way to dismiss keyboard?
I have quite a few controls scattered throughout many table cells in my table, and I was wondering if there's an easier way to dismiss the keyboard without having to loop through all my controls and ...
377
votes
4
answers
99k
views
How to write iOS app purely in C
I read here Learn C Before Objective-C?
Usually I then replace some Obj-C code with pure C code (after all you can mix them as much as you like, the content of an Obj-C method can be entirely, pure ...
369
votes
30
answers
252k
views
How do I add 1 day to an NSDate?
Basically, as the title says. I'm wondering how I could add 1 day to an NSDate.
So if it were:
21st February 2011
It would become:
22nd February 2011
Or if it were:
31st December 2011
It would ...
368
votes
24
answers
215k
views
How to use Auto Layout to move other views when a view is hidden?
I have designed my custom Cell in IB, subclassed it and connected my outlets to my custom class. I have three subviews in cell content which are: UIView (cdView) and two labels (titleLabel and ...
367
votes
26
answers
181k
views
Get the current first responder without using a private API
I submitted my app a little over a week ago and got the dreaded rejection email today. It tells me that my app cannot be accepted because I'm using a non-public API; specifically, it says,
The non-...
358
votes
16
answers
259k
views
How do I draw a shadow under a UIView?
I'm trying to draw a shadow under the bottom edge of a UIView in Cocoa Touch. I understand that I should use CGContextSetShadow() to draw the shadow, but the Quartz 2D programming guide is a little ...
357
votes
16
answers
61k
views
UITableView didSelectRowAtIndexPath: not being called on first tap
I'm having an issue with UITableView's didSelectRowAtIndexPath.
My table is setup so that when I select row it initializes a new view controller and pushes it.
The first time I tap any row in the ...
352
votes
34
answers
280k
views
UITableView - change section header color
How can I change color of a section header in UITableView?
EDIT: The answer provided by DJ-S should be considered for iOS 6 and above. The accepted answer is out of date.
349
votes
17
answers
346k
views
How to scale a UIImageView proportionally?
I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width.
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL ...
349
votes
21
answers
146k
views
iOS UIImagePickerController result image orientation after upload
I am testing my iPhone application on an iOS 3.1.3 iPhone. I am selecting/capturing an image using a UIImagePickerController:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] ...
346
votes
33
answers
124k
views
What are best practices that you use when writing Objective-C and Cocoa? [closed]
I know about the HIG (which is quite handy!), but what programming practices do you use when writing Objective-C, and more specifically when using Cocoa (or CocoaTouch).