Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I'm preparing to submit such a customization by subclassing UIAlerView. It's layout is entirely based on the given topography of the UIAlertView, have no read any private property. Is this kind of customization acceptable by App Store review process?
BGAlertViewWithSwitch.h
//
// BGAlertViewWithSwitch.h
// BGAlertViewWithSwitch
//
// Created by Borbas Geri on 11/7/11.
// Copyright 2011 ©ompactApps. All rights reserved.
//
#import <Foundation/Foundation.h>
//An assumed value.
#define ALERT_VIEW_LINE_HEIGHT 20.0
#define ALERT_VIEW_LABEL_PADDING 5.0
#define ALERT_VIEW_LABEL_ALPHA 0.5
#define kAlertSwitchLabelTag 42
#interface BGAlertViewWithSwitch : UIAlertView
{
UISwitch *_alertSwitch;
UILabel *_alertSwitchLabel;
}
#property (nonatomic, retain) UISwitch *alertSwitch;
#property (nonatomic, retain) UILabel *alertSwitchLabel;
#property (nonatomic, readonly, getter=isOn) BOOL on;
-(id)initWithTitle:(NSString*) title
message:(NSString*) message
switchMessage:(NSString*) switchMessage
delegate:(id) delegate
cancelButtonTitle:(NSString*) cancelButtonTitle
okButtonTitle:(NSString*) okButtonTitle;
#end
BGAlertViewWithSwitch.m
//
// BGAlertViewWithSwitch.m
// BGAlertViewWithSwitch
//
// Created by Borbas Geri on 11/7/11.
// Copyright 2011 ©ompactApps. All rights reserved.
//
#import "BGAlertViewWithSwitch.h"
#implementation BGAlertViewWithSwitch
#synthesize alertSwitch = _alertSwitch;
#synthesize alertSwitchLabel = _alertSwitchLabel;
#pragma mark - UISwitch Accessor
-(BOOL)isOn
{
return self.alertSwitch.isOn;
}
#pragma mark - View lifecycle
-(id)initWithTitle:(NSString*) title
message:(NSString*) message
switchMessage:(NSString*) switchMessage
delegate:(id) delegate
cancelButtonTitle:(NSString*) cancelButtonTitle
okButtonTitle:(NSString*) okButtonTitle
{
//For testing layout
NSString *placeHolder = #"";
//Append a line to the message that leaves the place for the switch.
NSString *_expandedMessage = [NSString stringWithFormat:#"%#\n%#\n%#\n", message, placeHolder, placeHolder];
if (self = [self initWithTitle:title
message:_expandedMessage
delegate:delegate
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:okButtonTitle, nil])
{
//Add switch.
self.alertSwitch = [[UISwitch alloc] init];
self.alertSwitch.on = YES;
[self addSubview:self.alertSwitch];
//Add label.
self.alertSwitchLabel = [[UILabel alloc] init];
self.alertSwitchLabel.text = switchMessage;
self.alertSwitchLabel.tag = kAlertSwitchLabelTag;
[self addSubview:self.alertSwitchLabel];
}
return self;
}
- (void)dealloc
{
self.alertSwitch = nil;
self.alertSwitchLabel = nil;
[super dealloc];
}
#pragma mark - Topography
- (void)layoutSubviews
{
NSLog(#"layoutSubviews to (%#)", NSStringFromCGRect(self.frame));
//Weak link to the message label.
UILabel *messageLabel;
//Enumerate subviews to find message label (the base of the topography).
for (UIView *eachSubview in self.subviews)
if ([[eachSubview class] isEqual:[UILabel class]])
{
UILabel *eachLabel = (UILabel*)eachSubview;
if (eachLabel.tag != kAlertSwitchLabelTag)
{
messageLabel = eachLabel;
NSLog(#"Each label frame (%#), saying '%#'", NSStringFromCGRect(eachLabel.frame), eachLabel.text);
}
}
//Center new content.
CGSize alertSwitchLabelSize = [self.alertSwitchLabel.text sizeWithFont:messageLabel.font];
float horizontalCentering = (messageLabel.frame.size.width - (alertSwitchLabelSize.width + ALERT_VIEW_LABEL_PADDING + self.alertSwitch.frame.size.width)) / 2;
//Switch goes to the bottom right.
float switchVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - self.alertSwitch.frame.size.height ) / 2;
CGRect alertSwitchFrame = CGRectMake(messageLabel.frame.origin.x + messageLabel.frame.size.width - self.alertSwitch.frame.size.width - horizontalCentering,
messageLabel.frame.origin.y + messageLabel.frame.size.height - self.alertSwitch.frame.size.height - switchVerticalCentering,
self.alertSwitch.frame.size.width,
self.alertSwitch.frame.size.height);
self.alertSwitch.frame = alertSwitchFrame;
//Label goes to the bottom left.
float switchLabelVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - ALERT_VIEW_LINE_HEIGHT ) / 2;
CGRect alertSwitchLabelFrame = CGRectMake(round( messageLabel.frame.origin.x + horizontalCentering ),
round( messageLabel.frame.origin.y + messageLabel.frame.size.height - ALERT_VIEW_LINE_HEIGHT - switchLabelVerticalCentering ),
messageLabel.frame.size.width - self.alertSwitch.frame.size.width,
ALERT_VIEW_LINE_HEIGHT); //self.alertSwitchLabel.frame.size.height);
self.alertSwitchLabel.frame = alertSwitchLabelFrame;
//Copy message label properties.
self.alertSwitchLabel.backgroundColor = [UIColor clearColor];
self.alertSwitchLabel.textColor = messageLabel.textColor;
self.alertSwitchLabel.font = messageLabel.font;
self.alertSwitchLabel.shadowColor = messageLabel.shadowColor;
self.alertSwitchLabel.shadowOffset = messageLabel.shadowOffset;
//Weaken.
self.alertSwitchLabel.alpha = ALERT_VIEW_LABEL_ALPHA;
[super layoutSubviews];
}
#end
The actual answer to the question is no -- Apple does not allow UIAlertView to be subclassed. From the UIAlertView docs:
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
Found here:
Subclassing UIAlertView
No one but Apple can adequately answer this question, so the best thing is to put it to the test. I think the main question you have to ask yourself is: Have I violated any provisions in the Apple Developer Agreement? If not, then submit your app. If you are worried about rejection, think of another way in which this could be done, as a backup, and be prepared to submit that in case of a problem.
Not that you have asked, but I would also opine that this change to Apple's design is not very intuitive. Do you mean the switch to mean "also delete from moquus?" as you already have a big delete button there. If the switch is off, then what does the delete button delete?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm developing a little app where in this app, have to have a voice who say some words.
Can I use the voice that some programs uses like "Google Translate", "Vozme" or similar? If not, how can I do this?
AVSpeechSynthesizer Class Reference is available from iOS7. The documentation is very good. Be sure to link the AVFoundation framework to your project.
Here is a working example that speaks text entered from a UITextField when a UIButton is tapped - (assuming a UIViewController sub-class named YOURViewController)
in .h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface YOURViewController : UIViewController <AVSpeechSynthesizerDelegate, UITextFieldDelegate> {
IBOutlet UITextField *textFieldInput;// connect to a UITextField in IB
}
- (IBAction)speakTheText:(id)sender;// connect to a UIButton in IB
#end
and in .m
#import "YOURViewController.h"
#interface YOURViewController ()
#end
#implementation YOURViewController
- (IBAction)speakTheText:(id)sender {
// create string of text to talk
NSString *talkText = textFieldInput.text;
// convert string
AVSpeechUtterance *speechUtterance = [self convertTextToSpeak:talkText];
// speak it...!
[self speak:speechUtterance];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (AVSpeechUtterance*)convertTextToSpeak:(NSString*)textToSpeak {
AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc] initWithString:textToSpeak];
speechUtterance.rate = 0.2; // default = 0.5 ; min = 0.0 ; max = 1.0
speechUtterance.pitchMultiplier = 1.0; // default = 1.0 ; range of 0.5 - 2.0
speechUtterance.voice = [self customiseVoice];
return speechUtterance;
}
- (AVSpeechSynthesisVoice*)customiseVoice {
NSArray *arrayVoices = [AVSpeechSynthesisVoice speechVoices];
NSUInteger numVoices = [arrayVoices count];
AVSpeechSynthesisVoice *voice = nil;
for (int k = 0; k < numVoices; k++) {
AVSpeechSynthesisVoice *availCustomVoice = [arrayVoices objectAtIndex:k];
if ([availCustomVoice.language isEqual: #"en-GB"]) {
voice = [arrayVoices objectAtIndex:k];
}
// This logs the codes for different nationality voices available
// Note that the index that they appear differs from 32bit and 64bit architectures
NSLog(#"#%d %#", k, availCustomVoice.language);
}
return voice;
}
- (void)speak:(AVSpeechUtterance*)speechUtterance {
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
speechSynthesizer.delegate = self;// all methods are optional
[speechSynthesizer speakUtterance:speechUtterance];
}
#end
I am experiencing a rather serious issue with my iPhone app using ARC.
I have a viewcontroller (lets call this A). This viewcontroller opens a navigationcontroller as a modal which runs through 3 different viewcontrollers (lets call these 1, 2 and 3). After viewing number 3 the navigationcontroller closes and we're back to A again.
So the flow is: A opens navigationcontroller and goes through 1->2->3 and then it closes again.
Every time I go through this flow i lose memory. I've searched through all my files looking for any retain og strong properties, un-invalidated timers or similar in order to solve this problem.
I have one idea, which might be the problem. At viewcontroller 1 i present a animation using coreanimation and a sprite. I'm using a implementation made by someone else. It seems like if i disable the animations the memory used seems quite constant (and thereby no memory loss). I have modified the implementation a bit to use ARC. This is the implementation I use for my sprite animations:
MCSpriteLayer.h
//
// MCSpriteLayer.h
//
// Created by Miguel Angel Friginal on 8/20/10.
// Copyright 2010 Mystery Coconut Games. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface MCSpriteLayer : CALayer {
unsigned int sampleIndex;
}
// SampleIndex needs to be > 0
#property (nonatomic) unsigned int sampleIndex;
// For use with sample rects set by the delegate
+ (id)layerWithImage:(CGImageRef)img;
- (id)initWithImage:(CGImageRef)img;
// If all samples are the same size
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size :(int)useRetina;
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
// Use this method instead of sprite.sampleIndex to obtain the index currently displayed on screen
- (unsigned int)currentSampleIndex;
#end
MCSpriteLayer.m
//
// MCSpriteLayer.m
//
// Created by Miguel Angel Friginal on 8/20/10.
// Copyright 2010 Mystery Coconut Games. All rights reserved.
//
#import "MCSpriteLayer.h"
#implementation MCSpriteLayer
#synthesize sampleIndex;
#pragma mark -
#pragma mark Initialization, variable sample size
- (id)initWithImage:(CGImageRef)img;
{
self = [super init];
if (self != nil)
{
self.contents = (__bridge id)img;
sampleIndex = 1;
}
return self;
}
+ (id)layerWithImage:(CGImageRef)img;
{
MCSpriteLayer *layer = [(MCSpriteLayer*)[self alloc] initWithImage:img];
return layer;
}
#pragma mark -
#pragma mark Initialization, fixed sample size
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
{
self = [self initWithImage:img];
if (self != nil)
{
CGSize sampleSizeNormalized = CGSizeMake(size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img));
self.bounds = CGRectMake( 0, 0, size.width, size.height );
self.contentsRect = CGRectMake( 0, 0, sampleSizeNormalized.width, sampleSizeNormalized.height );
}
return self;
}
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size :(int)useRetina;
{
CGSize newSampleSize;
if(useRetina == 1) {
// Supporting retina displays
if ([[UIScreen mainScreen] respondsToSelector:#selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
newSampleSize = CGSizeMake(size.width*2, size.height*2);
} else {
newSampleSize = size;
}
} else
newSampleSize = size;
MCSpriteLayer *layer = [[self alloc] initWithImage:img sampleSize:newSampleSize];
return layer;
}
#pragma mark -
#pragma mark Frame by frame animation
+ (BOOL)needsDisplayForKey:(NSString *)key;
{
return [key isEqualToString:#"sampleIndex"];
}
// contentsRect or bounds changes are not animated
+ (id < CAAction >)defaultActionForKey:(NSString *)aKey;
{
if ([aKey isEqualToString:#"contentsRect"] || [aKey isEqualToString:#"bounds"])
return (id < CAAction >)[NSNull null];
return [super defaultActionForKey:aKey];
}
- (unsigned int)currentSampleIndex;
{
return ((MCSpriteLayer*)[self presentationLayer]).sampleIndex;
}
// Implement displayLayer: on the delegate to override how sample rectangles are calculated; remember to use currentSampleIndex, ignore sampleIndex == 0, and set the layer's bounds
- (void)display;
{
if ([self.delegate respondsToSelector:#selector(displayLayer:)])
{
[self.delegate displayLayer:self];
return;
}
unsigned int currentSampleIndex = [self currentSampleIndex];
if (!currentSampleIndex)
return;
CGSize sampleSize = self.contentsRect.size;
self.contentsRect = CGRectMake(
((currentSampleIndex - 1) % (int)(1/sampleSize.width)) * sampleSize.width,
((currentSampleIndex - 1) / (int)(1/sampleSize.width)) * sampleSize.height,
sampleSize.width, sampleSize.height
);
}
#end
Is this implementation somehow not realeasing correctly or retaining anything? Thanks in advance.
Update
- I am using Instruments to measure the memory. I am using the Memory Monitor where I keep an eye on the Physical Memory Free
- The image is created like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"round_start.png" ofType:nil];
CGSize fixedSize = CGSizeMake(320, 480);
mascot = [MCSpriteLayer layerWithImage:[UIImage imageWithContentsOfFile:path].CGImage sampleSize:fixedSize :0];
mascot.frame = CGRectMake(ANIMATION_X, ANIMATION_Y, ANIMATION_WIDTH, ANIMATION_HEIGHT);
[self.view.layer addSublayer:mascot2];
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:#"sampleIndex"];
anim.delegate = self;
anim.fromValue = [NSNumber numberWithInt:1];
anim.toValue = [NSNumber numberWithInt:52];
anim.duration = ANIMATION_DURATION;
anim.repeatCount = 1;
[mascot addAnimation:anim forKey:nil];
- I've been experiencing with closing the modal with
[self dismissModalViewControllerAnimated:YES];
and
[self.navigationController dismissModalViewControllerAnimated:YES];
Dismissing the navigation controller does not release it, assuming you have a strong reference to it (that would need to get nil'd). In the view controllers used by it, add a log message in the dealloc method, so you know they are getting dealloced (you can do this for any subclassed item). If needed you can create a simple UINavigation subclass for the sole purpose of adding a message in dealloc). You will find that one or more of these items are not getting dealloced, and then you need to figure out if they are retained by a property/ivar or because they still have a superview.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am just about to start a new project where I have a custom menu that I need to display on everyview that I have. I dont want to use tab bars as this menu is custom designed and may have some animation added to it at some point.
Is there a simple way of creating this menu in one place so that I dont have to build it into every xib file??
Thanks
The tab bar controller is a system provided container controller. If you're using iOS 5 and later, you can make your own custom container view controller:
See Custom Container View Controllers discussion in the View Controller Programming Guide.
The key methods you need are enumerated in the UIViewController Class Reference, too.
I'd also suggest checking out WWDC 2011 #102 - Implementing UIViewController Containment.
Update:
If you want to write your own custom menu, you could do something like the following. I'm not doing anything fancy, but I'm just adding three colored subviews that might correspond to your custom buttons. And I have a tap gesture recognizer on each, which you can obviously handle as you see fit:
NSInteger const kHeight = 50;
NSInteger const kCount = 3;
#interface CustomMenu ()
#property (nonatomic, strong) NSMutableArray *menuViews;
#end
#implementation CustomMenu
- (id)init
{
self = [super init];
if (self)
{
_menuViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < kCount; i++)
{
UIView *subview = [[UIView alloc] init];
subview.tag = i;
[self addSubview:subview];
[_menuViews addObject:subview];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[subview addGestureRecognizer:recognizer];
}
[_menuViews[0] setBackgroundColor:[UIColor blueColor]];
[_menuViews[1] setBackgroundColor:[UIColor redColor]];
[_menuViews[2] setBackgroundColor:[UIColor greenColor]];
}
return self;
}
- (void)layoutSubviews
{
CGFloat width = self.superview.bounds.size.width;
CGFloat height = self.superview.bounds.size.height;
CGFloat menuChoiceWidth = width / kCount;
self.frame = CGRectMake(0, height - kHeight, width, kHeight);
NSInteger subviewIndex = 0;
for (UIView *subview in self.menuViews)
{
subview.frame = CGRectMake(subviewIndex * menuChoiceWidth, 0,
menuChoiceWidth, kHeight);
subviewIndex++;
}
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(#"%s tapped on %d", __FUNCTION__, recognizer.view.tag);
}
#end
Then, you various view controllers just need to make sure to add the CustomMenu to the view:
#interface ViewController ()
#property (nonatomic, strong) CustomMenu *menu;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.menu = [[CustomMenu alloc] init];
[self.view addSubview:self.menu];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.menu layoutSubviews];
}
#end
I confess that I've given up on iOS 4.3 support (it just isn't worth the heartache and the size of the 4.3 audience is pretty small nowadays), so I don't deal with this silliness any more, but hopefully this gives you a sense of what one possible solution might look like.
I have an app that uses a GoogleMap view.
Into that app, I want to display some custom anotations, and a custom view for the userLocation. When the location manager gives a heading, I want to display that custom view for userLocation, if it fails to deliver one, I want the default blue animated dot to come back, etc...
Project and Source code available HERE
Well, there are 2 problems here :
1st problem, THE REALLY BAD ONE. You can play for hours with that tiny app. But... Launch it, wait it loads, send it to the background, put your iPhone in sleep mode, wait a minute, wake up your iPhone, launch the app (that wakes it up) : crash, EXC_BAD_ACCESS. Do the same thing not putting the iPhone in sleep mode : no crash. Not waiting for a minute but a few seconds, no crash. 10 seconds, no crash, 20 seconds, no crash, nears 30 seconds, perhaps a crash, near a minute, crash !
2nd problem (bonus :-) ), that can be linked with the first one, but that is not really the heart of this question : the solution I use to achieve the userPinLocation to update from/to the blue pin seems to have a really bad side effect : the userLocation does not move on the map as you move in the street. For any answer for that 2nd problem, if it's not directly linked with the crash, please use comments not answers. This is not the heart of the question... I think...
To find the faulty code, I've reduced my app at its minimum. That gives the following code (some tips in the source code as comments) :
EXC_BAD_ACCESS_TestViewController.h
#import <MapKit/MapKit.h>
#interface EXC_BAD_ACCESS_TestViewController : UIViewController<CLLocationManagerDelegate> {
CLLocationManager* locationMgr;
NSMutableArray* customAnnotations;
CLLocationDirection userHeading;
MKMapView* myMapView;
}
#property(nonatomic, retain) CLLocationManager* locationMgr;
#property(nonatomic, retain) NSMutableArray* customAnnotations;
#property(nonatomic, assign) CLLocationDirection userHeading;
#property(nonatomic, retain) IBOutlet MKMapView* myMapView;
- (void) loadAnnotations;
#end
EXC_BAD_ACCESS_TestViewController.m
// On first launch, due to code simplification, the app may crash when asked authorization to use geo hardware. Just kill/relaunch after accepting.
// Breakpoints on each method entry : EXC_BAD_ACCESS crash without any break-stop
#import "EXC_BAD_ACCESS_TestViewController.h"
#import "CustomAnnotation.h"
#implementation EXC_BAD_ACCESS_TestViewController
#synthesize locationMgr, customAnnotations, myMapView, userHeading;
// ===========================================================================================================
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) return nil;
self.userHeading = -1;
return self;
}
// ===========================================================================================================
- (void) viewDidLoad
{
[self loadAnnotations];
self.myMapView.mapType = MKMapTypeStandard;
self.myMapView.showsUserLocation = YES;
// -----------------------------------------------
// Just comment this sole line : no more crash
// -----------------------------------------------
for (CustomAnnotation* pin in self.customAnnotations) [self.myMapView addAnnotation:pin];
// locationManager
self.locationMgr = [[CLLocationManager alloc] init];
self.locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
self.locationMgr.distanceFilter = 1.0;
self.locationMgr.headingFilter = 1.0;
self.locationMgr.purpose = #"Some purpose.";
self.locationMgr.delegate = self;
[self.locationMgr startUpdatingHeading];
[super viewDidLoad];
}
// ===========================================================================================================
- (void) loadAnnotations
{
// Code for testing, real code gets the datas using another way of doing, as simple as this one
self.customAnnotations = [NSMutableArray array];
double latitude = 45.0;
double longitude = -45.0;
for (int i=1; i<=400; i++) {
CLLocationCoordinate2D pinLocation = CLLocationCoordinate2DMake(latitude, longitude);
CustomAnnotation* pin = [[[CustomAnnotation alloc] initWithCoordinate:pinLocation] autorelease];
[self.customAnnotations addObject:pin];
latitude += 0.01;
longitude += 0.01;
}
}
// ===========================================================================================================
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKAnnotationView* pinView = nil;
NSString* annotationIdentifier;
UIImage* pinImage;
BOOL canShowCallout;
// User location
if ([annotation isKindOfClass:[MKUserLocation class]] && self.userHeading >= 0) {
annotationIdentifier = #"UserLocationAnnotation";
pinImage = [UIImage imageNamed:#"custom_userlocation.png"];
canShowCallout = NO;
}
// Custom Pin
else if ([annotation isKindOfClass:[CustomAnnotation class]]) {
annotationIdentifier = #"CustomAnnotation";
pinImage = [UIImage imageNamed:#"custom_pin.png"];
canShowCallout = YES;
}
// Others
else {
return nil;
}
pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (pinView) {
pinView.annotation = annotation;
}
else {
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease];
if (pinView) {
pinView.image = pinImage;
pinView.canShowCallout = canShowCallout;
if (canShowCallout) {
pinView.calloutOffset = CGPointMake(-5, 5);
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
}
}
return pinView;
}
// -----------------------------------------------
// Just comment this method : no more crash
// -----------------------------------------------
// ===========================================================================================================
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if (!self.myMapView) return;
if (!self.myMapView.userLocation) return;
CLLocationDirection compassHeading_True = newHeading.trueHeading;
CLLocationDirection compassHeading_Magnetic = newHeading.magneticHeading;
CLLocationDirection heading;
if (compassHeading_True == -1) heading = compassHeading_Magnetic;
else if (newHeading.headingAccuracy >= 0) heading = compassHeading_True;
else heading = -1;
// If we get/loose the heading, update pin image
// I didn't found any other solution to force the user pin to update its display.
if ((self.userHeading == -1 || heading == -1) && self.userHeading != heading) {
[self.myMapView removeAnnotation:self.myMapView.userLocation];
self.userHeading = heading;
[self.myMapView addAnnotation:self.myMapView.userLocation];
}
self.userHeading = heading;
// ------------------------------------
// Some non bugued code was there
// ------------------------------------
}
// ===========================================================================================================
- (void) dealloc
{
self.myMapView = nil;
self.customAnnotations = nil;
self.locationMgr = nil;
[super dealloc];
}
#end
CustomAnnotation.h
#import <MapKit/MapKit.h>
#interface CustomAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
}
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D) c;
#end
CustomAnnotation.m
#import "CustomAnnotation.h"
#implementation CustomAnnotation
#synthesize coordinate;
// ===========================================================================================================
- (id)initWithCoordinate:(CLLocationCoordinate2D) c
{
self = [super init];
if (!self) return nil;
self.coordinate = c;
return self;
}
// ===========================================================================================================
- (NSString*)subtitle
{
return #"";
}
// ===========================================================================================================
- (NSString*)title
{
return #"";
}
#end
I tried breakpoints in each methods, trying to enable Zombies with environment variables (but as this needs to be ran on the device, I'm not sure they've been really activated), without any start of solution... I still don't have any idea of what's going wrong.
Do you see how to solve that EXC_BAD_ACCESS problem ?
For any answer for the 2nd problem, please use comments and not answers if it don't solve the crash. This problem is not the heart of the question as far as I've seen.
Tip : I've put 2 comments in the code above where I found some start of solution. There are 2 places, that does not seems connected, that can be put out of the code one OR the other to solve the problem. What makes me crazy is the OR.
Runned on an iPhone 4 ios 4.2.1
[self.myMapView removeAnnotation:self.myMapView.userLocation];
This line is causing the crash from stacktrace this is what I've seen.
When I saw exc_bad_access I did a bt in gdb. I have added Guard Malloc breakpoint. Looking at the stack it said about removing annotation.
You can try overriding a method called didReceiveMemoryWarning in your view controller .m file.
What happens sometimes is that memory on the device gets low due to running apps and IOS sends a message to the app. Now the trouble is when you don't override memory warning method, its default action is to remove any subviews from memory which are not visible. This can lead to zombie variables when your application runs.
Check out the apple doc
The iPad programming guide says that the splitView's left pane is fixed to 320 points. But 320 pixels for my master view controller is too much. I would like to reduce it and give more space to detail view controller. Is it possible by anyway?
Link to the document which speaks about fixed width.
If you subclass UISplitViewController, you can implement -viewDidLayoutSubviews and adjust the width there. This is clean, no hacks or private APIs, and works even with rotation.
- (void)viewDidLayoutSubviews
{
const CGFloat kMasterViewWidth = 240.0;
UIViewController *masterViewController = [self.viewControllers objectAtIndex:0];
UIViewController *detailViewController = [self.viewControllers objectAtIndex:1];
if (detailViewController.view.frame.origin.x > 0.0) {
// Adjust the width of the master view
CGRect masterViewFrame = masterViewController.view.frame;
CGFloat deltaX = masterViewFrame.size.width - kMasterViewWidth;
masterViewFrame.size.width -= deltaX;
masterViewController.view.frame = masterViewFrame;
// Adjust the width of the detail view
CGRect detailViewFrame = detailViewController.view.frame;
detailViewFrame.origin.x -= deltaX;
detailViewFrame.size.width += deltaX;
detailViewController.view.frame = detailViewFrame;
[masterViewController.view setNeedsLayout];
[detailViewController.view setNeedsLayout];
}
}
In IOS 8.0 you can easily do this by doing the following:
1. In your MasterSplitViewController.h add
#property(nonatomic, assign) CGFloat maximumPrimaryColumnWidth NS_AVAILABLE_IOS(8_0);
2. In your MasterSplitViewController.m viewDidLoad method add
self.maximumPrimaryColumnWidth = 100;
self.splitViewController.maximumPrimaryColumnWidth = self.maximumPrimaryColumnWidth;
This is a really good, simple and easy feature of IOS 8.
this code is work for me
[splitViewController setValue:[NSNumber numberWithFloat:200.0] forKey:#"_masterColumnWidth"];
No.
There are two private properties
#property(access,nonatomic) CGFloat masterColumnWidth;
#property(access,nonatomic) CGFloat leftColumnWidth; // both are the same!
but being private mean they can't be used for AppStore apps.
iOS 8 introduced a new property:
// An animatable property that can be used to adjust the maximum absolute width of the primary view controller in the split view controller.
#property(nonatomic, assign) CGFloat maximumPrimaryColumnWidth NS_AVAILABLE_IOS(8_0); // default: UISplitViewControllerAutomaticDimension
Use this property to adjust your master viewcontroller to your desired width.
Here is how I did this in iOS8 with Swift.
class MainSplitViewController: UISplitViewController {
override func viewDidLoad() {
self.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
self.maximumPrimaryColumnWidth = 100 // specify your width here
}
}
If you need to change the width dynamically from within your master/detail view in the split view, then do something like this:
var splitViewController = self.splitViewController as MainSplitViewController
splitViewController.maximumPrimaryColumnWidth = 400
The storyboard way would be this one, mentioned by #Tim:
Furthermore, if you want the Master view to always take up a certain percentage of the screen then you can use the Key Path = "preferredPrimaryColumnWidthFraction" instead and set the value to 0.2 (for 20% screen size).
Please note that the "maximumPrimaryColumnWidth" is set to 320, so if you try the screen percent value of 0.5 (50%) it won't go above 320. You can add a key path for maximumPrimaryColumnWidth if you need to override this.
None of the answers worked for me on iOS7, so I did some of my own research and created a working solution. This will involve subclassing UISplitViewController for the full functionality.
I will present the answer as if we just created a new project for iPad with all device orientations and have set the custom UISplitViewController as the main view controller.
Create your custom UISplitViewController. In this example mine is called MySplitViewController. All code will be based in MySplitViewController.m.
We're going to need to access a method from the UISplitViewControllerDelegate so add that and set the delegate. We'll also setup a delegate forwarder incase you need to call the delegate methods from another class.
#interface MySplitViewController () <UISplitViewControllerDelegate>
#property (nonatomic, weak) id<UISplitViewControllerDelegate> realDelegate;
#end
#implementation MySplitViewController
- (instancetype)init {
self = [super init];
if (self) {
self.delegate = self;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.delegate = self;
}
return self;
}
- (void)setDelegate:(id<UISplitViewControllerDelegate>)delegate {
[super setDelegate:nil];
self.realDelegate = (delegate != self) ? delegate : nil;
[super setDelegate:delegate ? self : nil];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
id delegate = self.realDelegate;
return [super respondsToSelector:aSelector] || [delegate respondsToSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
id delegate = self.realDelegate;
return [delegate respondsToSelector:aSelector] ? delegate : [super forwardingTargetForSelector:aSelector];
}
Setup the master and detail view controllers.
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController* masterViewController = [[UIViewController alloc] init];
masterViewController.view.backgroundColor = [UIColor yellowColor];
UIViewController* detailViewController = [[UIViewController alloc] init];
detailViewController.view.backgroundColor = [UIColor cyanColor];
self.viewControllers = #[masterViewController, detailViewController];
}
Lets add our desired width to a method for easy reference.
- (CGFloat)desiredWidth {
return 200.0f;
}
We'll manipulate the master view controller before presenting it.
- (void)splitViewController:(UISplitViewController *)svc popoverController:(UIPopoverController *)pc willPresentViewController:(UIViewController *)aViewController {
id realDelegate = self.realDelegate;
if ([realDelegate respondsToSelector:#selector(splitViewController:popoverController:willPresentViewController:)]) {
[realDelegate splitViewController:svc popoverController:pc willPresentViewController:aViewController];
}
CGRect rect = aViewController.view.frame;
rect.size.width = [self desiredWidth];
aViewController.view.frame = rect;
aViewController.view.superview.clipsToBounds = NO;
}
However, now we're left with a display like this.
So were going to override a private method. Yes a private method, it will still be acceptable in the App Store since its not an underscore private method.
- (CGFloat)leftColumnWidth {
return [self desiredWidth];
}
This deals with portrait mode. So a similar thing for -splitViewController:willShowViewController:invalidatingBarButtonItem: and you should be set for landscape.
However none of this will be needed in iOS8. You'll be able to simply call a min and max width property!
use the following code before assigning to the rootviewcontroller. It works for me with ios7
[self.splitViewController setValue:[NSNumber numberWithFloat:256.0] forKey:#"_masterColumnWidth"];
self.window.rootViewController = self.splitViewController;
Since no one mentioned that this can be done from IB, I want to add this answer. Apparently, you can set "User Defined Runtime Attributes" for the UISplitViewContorller with following details:
Key Path:masterColumnWidth
Type: Number
Value: 250
In my case, I had to set both maximum and minimum to make this work
mySplitViewController.preferredDisplayMode = .allVisible;
mySplitViewController.maximumPrimaryColumnWidth = UIScreen.main.bounds.width/2;
mySplitViewController.minimumPrimaryColumnWidth = UIScreen.main.bounds.width/2;
You can use GSSplitViewController. This one will work on iOS 7 and 8
splitView = [[GSSplitViewController alloc] init];
splitView.masterPaneWidth = 180;
You can also include it by adding pod 'GSSplitViewController' to your Podfile.
ViewController.h
#property(nonatomic, assign) CGFloat maximumPrimaryColumnWidth NS_AVAILABLE_IOS(8_0);
ViewController.m
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if (SYSTEM_VERSION_LESS_THAN(#"10.0")) {
[self setValue:[NSNumber numberWithFloat:200.0]forKey:#"_masterColumnWidth"];
}else{
self.maximumPrimaryColumnWidth = 200;
self.splitViewController.maximumPrimaryColumnWidth = self.maximumPrimaryColumnWidth;
}
Swift 3.0 you use like
let widthfraction = 2.0 //Your desired value for me 2.0
splitViewController?.preferredPrimaryColumnWidthFraction = 0.40
let minimumWidth = min((splitViewController?.view.bounds.size.width)!,(splitViewController?.view.bounds.height)!)
splitViewController?.minimumPrimaryColumnWidth = minimumWidth / widthFraction
splitViewController?.maximumPrimaryColumnWidth = minimumWidth / widthFraction
let leftNavController = splitViewController?.viewControllers.first as! UINavigationController
leftNavController.view.frame = CGRect(x: leftNavController.view.frame.origin.x, y: leftNavController.view.frame.origin.y, width: (minimumWidth / widthFraction), height: leftNavController.view.frame.height)
// in UISplitViewController subclass
// let more space for detail in portrait mode
- (void)viewWillLayoutSubviews
{
CGFloat width;
if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication.statusBarOrientation)){
width = CGRectGetWidth(self.view.bounds) * 0.25f;
}
else {
width = CGRectGetWidth(self.view.bounds) * 0.33f;
}
width = (NSInteger)fminf(260, fmaxf(120, width));
self.minimumPrimaryColumnWidth = width;
self.maximumPrimaryColumnWidth = width;
[super viewWillLayoutSubviews];
}
This code work for me:)
#interface UISplitViewController(myExt)
- (void)setNewMasterSize:(float)size;
#end
#implementation UISplitViewController(myExt)
- (void)setNewMasterSize:(float)size
{
_masterColumnWidth = size;
}
#end
and use it on each operation with view (like rotation)