how to enlarge an image view within the view which is animating - ios

I am trying to enlarge the size of an UIImageView which is inside a view.
Now the problem is I am showing this view with animation and it's becoming larger from small in size however the image is not enlarging. To make this UIImageView larger I am writing the same code which I wrote for view; that is
self.alertView.frame = CGRectMake(30, 100, 235, 190);
self.alertImgView.frame = CGRectMake(20, 20, 150, 150);
and also i want to show this image view in between of this view that is not happening either. Please suggest

Have you tried this :-
[self.alertImgView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];

Related

Swift - Issues with setting background image in a UIScrollView

I have created a UIScrollView and added UILabels and UIButtons. I then have created a UIImageView and added it to the my UIScrollView. I have used the following code to set the UIImageView to the background of my UIScrollView.
let paperImageView = UIImageView(image: UIImage(named: "White Notebook Paper.png"))
paperImageView.frame = CGRect(x: -60, y: -60, width: UIScreen.main.bounds.width + 60, height: currentY + 60)
scrollView.addSubview(paperImageView)
scrollView.sendSubview(toBack: paperImageView)
As you can see in the following screenshot, by UIImageView is distorted.
The background image supposed to look like notebook paper. As you can see, the spacing of the lines becomes wider the lower you go in the UIScrollView. Thanks for taking the time to share your thoughts.
I forgot to change the content mode of the image view.
paperImageView.contentMode = .scaleAspectFill

Scaling down a UIButton's background image when using initWithFrame:

This is the first time I have ever designed an iOS app so I want to make sure I understand this behavior correctly.
I designed a custom bar button icon for a navigation bar in Photoshop. The final image that I saved in Photoshop was 102 x 45, and yes I realize that these dimensions are bigger than the recommended 44x44 in the iOS 7 design guidelines.
Anyways, I placed my image into the asset folder, and then programmatically set the bar button item with the following code:
UIImage* firstButtonImage = [UIImage imageNamed:#"loginbutton1"];
CGRect frame = CGRectMake(0, 0, 102, 45);
UIButton * someButton = [[UIButton alloc] initWithFrame:frame];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(didTapLoginButton:)
forControlEvents:UIControlEventTouchUpInside];
self.rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navItem.rightBarButtonItem = self.rightBarButton;
As you can see I set the frame's width and height to the exact size of the image. When I first ran the app, I didn't like the image and thought it was too big. So I changed the width and height parameters in this statement:
CGRect frame = CGRectMake(0, 0, 70, 30);
And now the image looks perfect on the iPhone screen. This is on an iPhone 4s.
So my main question is, what is actually happening when I change the frame size? Since the frame is now smaller than the actual image size, does the image just get scaled down automatically to fit inside the frame?
Yes the image get scaled because you are using backgroundImage (not Image). Both images have different behaviors.
Check the Xcode Interface Builder, you can see there, that you can set two images: Image and Background. Background is the UIImage that get scaled for the whole frame of the UIButton.
The UIButton Class Reference allows you to access the imageView of the image (not theimageView of the backgroundImage)
Because you have access to the imageView, you can change the mode of the image with:
[[someButton imageView] setContentMode:UIViewContentModeBottomLeft];
In UIView Class Reference you can check all the UIViewContentModes provided by Apple.
You can check that changing a little bit your code:
[someButton setImage:firstButtonImage forState:UIControlStateNormal];
[[someButton imageView] setContentMode:UIViewContentModeBottomRight];

Programmatically create subview that covers the top 50% of the iPhone screen

I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.
I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.
Here is what I am currently doing:
UIImageView *halfView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:halfView];
halfView.frame = CGRectMake(0, 0, 320, 270);
Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?
Here is what I want the subview to look like on the iphone's screen:
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);
you just take the height and divide it by two
You should also probably change 320 to self.view.bounds.size.width
I suggest reading this post to really get a grasp of UIViews and working with them:
UIView frame, bounds and center

Autoresizing Mask Strange Behavior

I'm in an UIViewController, self.view points to a valid view with a frame of 300x480.
UIView *redView;
UIView *blueView;
//[[self view] setAutoresizingMask:YES]; //WRONG!
[[self view] setAutoresizesSubviews:YES];
[[self view] setClipsToBounds:YES];
redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[redView setBackgroundColor:[UIColor redColor]];
[redView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[[self view] addSubview:redView];
blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[blueView setBackgroundColor:[UIColor blueColor]];
[blueView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
[[self view] addSubview:blueView];
With this snippet of code i was trying
To align the 200x200 red view to the right margin,
To align the 100x100 blue view to the left margin;
but the result as you can see is far from being what i was expecting...
http://imageshack.us/photo/my-images/27/iossimulatorscreenshoto.png/
I've read every single bit of apple documentation and every google result about the autoresizing mask usage, but i still can't figure out why this is happening.
Can someone please explain to me what is happening?
Thanks in advance.
Keep in mind it's an autoREsizing mask, not an autosizing mask. It doesn't do anything until something changes size.
What you want to do is define your views' frames exactly where you want them to be according to the current size of your parent view, i.e. 300 x 480. So for example
redView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 200, 200)];
blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
You are setting their autoresizing masks correctly as far as the horizontal alignment. If self.view changes size, then redView and blueView will remain locked to the margins.
You should set at least one of flexible top margin, flexible bottom margin, or flexible height. Otherwise the behavior is undefined if the parent view changes height. It's impossible for all three of those parameters to remain fixed within two different heights.
This line of code doesn't make sense:
[[self view] setAutoresizingMask:YES];
It isn't a compiler error because BOOL happens to be castable to UIViewAutoresizing, which is an enum. The effect is that YES = 1 = UIViewAutoresizingFlexibleLeftMargin. Probably not what you wanted. More likely you wanted:
[[self view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
If you can, you should do all this in Interface Builder. Since it's a WYSIWYG editor, you can SWYG ("see what you get"). You can quickly play around with the options and learn how they work. Much faster than edit - compile - run to see the effects. Even if you can't / won't use Interface Builder for your project, try a sample project with it to learn how autoresizing works. Once you know you can do it in code with less trial and error. But watch out for the gotcha that turning a margin bar on in Interface Builder is equivalent to turning the corresponding FlexibleXYZMargin option off in code.

Applying transform to UITextView - prevent content resizing

When I apply a rotation transform to a UITextView and then click inside to begin editing, it appears that the content size is automatically being made wider. The new width of the content view is the width of the rotated view's bounding box. For example, given a text box of width 500 and height 400, and rotated by 30 degrees, the new content width would be:
(500 * cos(30)) + (400 * sin(30)) = 633
Or graphically:
Interestingly, if you are already editing the text view and THEN apply the transform, then it appears that no modification is made to the content size. So it appears that sometime around the start of text editing, the text view looks at its frame property and adjusts the content size based on the frame width. I imagine the solution to this is to tell it to use the bounds property instead, however I don't know where to do this, as I'm not sure exactly where the text view is deciding to modify the content size.
I have googled but can't seem to find any references to using transformed UITextViews. Does anybody have any ideas about this?
EDIT (button action from test project):
- (IBAction)rotateButtonTapped:(id)sender {
if (CGAffineTransformIsIdentity(self.textView.transform)) {
self.textView.transform = CGAffineTransformMakeRotation(30.0 * M_PI / 180.0);
}
else {
self.textView.transform = CGAffineTransformIdentity;
}
NSLog(#"contentsize: %.0f, %.0f", textView.contentSize.width, textView.contentSize.height);
}
I was also stuck with this problem.
The only solution which I found was to create an instance of UIView and add the UITextView as a subview. Then you can rotate the instance of UIView and UITextView will work just fine.
UITextView *myTextView = [[UITextView alloc] init];
[myTextView setFrame:CGRectMake(0, 0, 100, 100)];
UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(20, 20, 100, 100)];
[myRotateView setBackgroundColor:[UIColor clearColor]];
[myRotateView addSubview:myTextView];
myRotateView.transform = CGAffineTransformMakeRotation(0.8);
[[self view] addSubview:myRotateView];
Have you tried applying the rotation by doing a layer transform rather than a transform on the view?
#import <QuartzCore/QuartzCore.h>
mytextField.layer.transform = CATransform3DMakeRotation (angle, 0, 0, 1);
This might be enough to trick whatever broken logic exists inside the core text field code.

Resources