addSubview crashes iOS 5.1 but not 6.1 - ios

The following works fine in 6.1 but the app crashes in 5.1...
weeklyHeaderViewController =[[WeeklyHeaderViewController alloc] init];
[self.view addSubview:weeklyHeaderViewController.view];
This is an iPad app and the view is 939x31 .xib containing 7 UILabel pairs. The controller'r viewDidLoad method inserts a 4x30 image seperator between the 7 pairs then moves the view into position at the bottom of the screen.
I can trace it through the initWithNibName's so it appears to be alloc ining fine. It crashes with at the addSubview and never gets to the viewDidLoad method.
Is there is something I should here that I am missing with regard to 5.1?
Any suggestions would be appreciated.
Thanks,
John
// WeeklyHeaderViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect myImageRect = CGRectMake(131, 0, 4, 30);
UIImageView *image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(266, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
self.view addSubview:image];
[image release];
myImageRect = CGRectMake(400, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(535, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(670, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(804, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
self.view.frame = CGRectMake(74, 687-14, self.view.frame.size.width, self.view.frame.size.height);
}

Thanks to iOSBegginer who reminded me to uncheck AutoLayout in my xib.

Related

Image crop stretching image from camera

I'm using this project: http://code4app.net/ios/Perfect-Image-Cropper/51de11576803fa4548000004 for cropping images.
The fact is that is working perfect with photos imported from Library, but not with image from camera.
When selecting image from camera it's showing cropped but stretched.
#interface ImageViewController (){
ImageCropperView *cropper;
UIView *containerView;
UIImageView *finImageView;
UIButton *removeBtn;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// Media is an image
UIImage *image = info[UIImagePickerControllerOriginalImage];
if (image != nil) {
[self resetCropper];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubview:containerView];
cropper = [[ImageCropperView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)]
[cropper setup];
cropper.image = image;
// Adding the crop button
UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cropBtn addTarget:self action:#selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
[cropBtn setTitle:#"Select" forState:UIControlStateNormal];
cropBtn.frame = CGRectMake(140, 160, 80, 30.0);
[containerView addSubview:cropBtn];
[containerView addSubview:cropper];
}
}
}
// Action when crop button clicked
- (IBAction)cropImage:(id)sender
{
[cropper finishCropping];
UIImage *newImage = cropper.croppedImage;
finImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
finImageView.image = newImage;
[cropper removeFromSuperview];
[containerView removeFromSuperview];
[self.scrollView insertSubview:finImageView belowSubview:self.actionsTab];
// Adding a button for remove
removeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeBtn.frame = CGRectMake( 0, 0, 22, 22);
[removeBtn setBackgroundImage:[UIImage imageNamed:#"remove"] forState:UIControlStateNormal];
[removeBtn addTarget:self action:#selector(removePhoto:) forControlEvents:UIControlEventTouchUpInside];
[finImageView insertSubview:removeBtn aboveSubview:finImageView];
finImageView.userInteractionEnabled = YES;
}
- (IBAction)removePhoto:(id)sender{
[self resetCropper];
[finImageView removeFromSuperview];
}
- (void)resetCropper
{
[cropper reset];
finImageView.image = nil;
[removeBtn removeFromSuperview];
}
Any help will be very appreciate!

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

How to set background image fixed while using Horizontal scrollview in ios

- (void)viewDidLoad
{
NSLog(#"Welcome to Home Page");
[super viewDidLoad];
self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"bg-image.png"]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(1400, 100);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti01.png"]];
tempImageView2.frame=CGRectMake(10, 60, 200, 200);
[scrollView addSubview:tempImageView2];
UIImageView *tempImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti02.png"]];
tempImageView3.frame=CGRectMake(240, 60, 200, 200);
[scrollView addSubview:tempImageView3];
UIImageView *tempImageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti03.png"]];
tempImageView4.frame=CGRectMake(470, 60, 200, 200);
[scrollView addSubview:tempImageView4];
UIImageView *tempImageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti04.png"]];
tempImageView5.frame=CGRectMake(700, 60, 200, 200);
[scrollView addSubview:tempImageView5];
UIImageView *tempImageView6 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti05.png"]];
tempImageView6.frame=CGRectMake(930, 60, 200, 200);
[scrollView addSubview:tempImageView6];
UIImageView *tempImageView7 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sti06.png"]];
tempImageView7.frame=CGRectMake(1160, 60, 200, 200);
[scrollView addSubview:tempImageView7];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
[scrollView addSubview:tempImageView3];
[scrollView addSubview:tempImageView4];
[scrollView addSubview:tempImageView5];
[scrollView addSubview:tempImageView6];
[scrollView addSubview:tempImageView7];
scrollView.userInteractionEnabled = YES;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(22, 100, 1800, 500);
// [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btn];
}
- (IBAction)buttonTest:(id)sender {
MSDescriptionpage *aSecondPageController = [[MSDescriptionpage alloc] initWithNibName:#"MSDescriptionpage" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
[aSecondPageController release];
}
How can i set back ground image fixed when i scroll THE CONTENT USING OF UISCROLLVIEW.How to set background image fixed while using Horizontal scrollview in ios.i using many formats but not issue was fixed..
EDIT
Now add the IBOutlate for the scrollView like this .h file
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
and in .m file
#synthesize scrollView;
and in xib Control-Drag from file's Owner to the scrollView to assign the IBOutlate to it
this will looks like this
Now

how to place different UIImages

Depending on the result of some user input, i have to show from 0 to n number of images [lines in a chart style background] in a
view, changing on the x axis,
the problem is that i dont know before hand how many images are going to be needed to draw,
so i cannot create all the UIImageViews needed [nor is elegant coding]
here an example of what I mean
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(12, 30, 12, 200)] autorelease];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
//[img release];
UIImageView *img2 = [[[UIImageView alloc] initWithFrame:CGRectMake(102, 30, 12, 200)] autorelease];
[img2 setImage:[UIImage imageNamed:#"line.png"]];
img2.opaque = YES;
[self.view addSubview:img2];
UIImageView *img3 = [[[UIImageView alloc] initWithFrame:CGRectMake(202, 30, 12, 200)] autorelease];
[img3 setImage:[UIImage imageNamed:#"line.png"]];
img3.opaque = YES;
[self.view addSubview:img3];
UIImageView *img4 = [[[UIImageView alloc] initWithFrame:CGRectMake(302, 30, 12, 200)] autorelease];
[img4 setImage:[UIImage imageNamed:#"line.png"]];
img4.opaque = YES;
[self.view addSubview:img4];
so how to accomplish this?
thanks a lot!
If in height you have height of your view, than:
for (int x=2; x<height; x+=100)
{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 30, 12, 200)];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
[img release];
}

Resources