How to rotate a UITextView without it resizing? - ios

I am trying to rotate a UITextView in my VC. When I try to rotate it the UITextView resizes its?
Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.title = #"Signature View";
[self.signatureView setLineWidth:2.0];
self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000];
NSLog(#"contentsize: %.0f, %.0f", self.view.frame.size.width, self.view.frame.size.height); <-- this is (contentsize: 320, 568)
self.howToTextView.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
[self.howToTextView setNeedsDisplay]; <-- to make it redraw
[self.howToTextView setTransform:CGAffineTransformMakeRotation(-90* M_PI/180)];
NSLog(#"contentsize: %.0f, %.0f", self.howToTextView.contentSize.width, self.howToTextView.contentSize.height); <--- turns out to be (contentsize: 103, 138)same as on storyboard design
}
UPDATE(With this code the uitextview is in place and the frame is right, but not the text won't resize to fill up the uitextviews new frame size. Its stuck down in the bottom corner?):
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.title = #"Signature View";
[self.signatureView setLineWidth:2.0];
self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000];
NSLog(#"contentsize: %.0f, %.0f", self.view.frame.size.width, self.view.frame.size.height);
self.howToTextView = [[UITextView alloc] init];
self.howToTextView.backgroundColor = [UIColor redColor];
self.howToTextView.text = #"this is a good day. i am going to make millions today. Smile while on the phone as it is the best way to increase your business. I hope that you like this app as we have work really hard on it. Please sign your name or decline but if you decline then you understand you lose all coverage.";
[self.view addSubview:self.howToTextView];
[self.howToTextView setTransform:CGAffineTransformMakeRotation(-90* M_PI/180)];
self.howToTextView.frame = CGRectMake(0, 0, 80, self.view.frame.size.height);
[self.howToTextView setNeedsDisplay];
NSLog(#"contentsize: %.0f, %.0f", self.howToTextView.contentSize.width, self.howToTextView.contentSize.height);
}
UPDATE 2: Sticking the textview inside a UIView makes the content size right, but now with this code the uitextview/uiview are in the top right corner of the app, even though I set them both to be 0,0 for x,y? Not sure why that is?
self.howToTextView = [[UITextView alloc] init];
[self.howToTextView setFrame:CGRectMake(0, 0, self.view.frame.size.height-20, 110)];
self.howToTextView.backgroundColor = [UIColor redColor];
self.howToTextView.text = #"this is a good day. i am going to make millions today. Smile while on the phone as it is the best way to increase your business. I hope that you like this app as we have work really hard on it. Please sign your name or decline but if you decline then you understand you lose all coverage.";
UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(0, 0, self.view.frame.size.height, 120)];
[myRotateView setBackgroundColor:[UIColor greenColor]];
[myRotateView addSubview:self.howToTextView];
myRotateView.transform = CGAffineTransformMakeRotation(-90* M_PI/180);
[[self view] addSubview:myRotateView];

After two days of struggling, I found a workaround:
Create a UIView.
Add your UITextView as a sub view to it.
Get the proper size of UITextView, using sizeThatFits().
Set UITextView frame.
Set UIView frame with 0 heigh and 0 width (it's important!).
Rotate your UIView.
Now if you want to change font size do it like this:
myTextView.font = UIFont.boldSystemFontOfSize(size)
let maxsize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)
let frame = myTextView.sizeThatFits(maxsize)
myTextView.frame = CGRectMake(0, 0, frame.width, frame.height)
At end rotate the UIView again.

Related

UIImageView size changes

I have navigation bar with custom view on it. UIImageView and UILabel are subviews of titleView. And I add UITapGestureRecognizer to titleView to make it show another UIViewController when the user taps on it. When you tap on titleView other UIViewController opens. But when you click on back button in my titleView size of UIImageView changes. Here is the code
UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:#"#474747"]];
[title setFont:[UIFont fontWithName:#"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;
Here are the images where you can see the changes
I check your code Add clips to bounds and do not need to title frame two times.
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.clipsToBounds = true;
Rather than setting the frame of the views directly, you might try using Auto Layout; I suspect what's happening is that your custom view is getting re-layed-out after the back button, and it's reverting to the image view's instrinsicSize.
What you could do instead is turn off translatesAutoresizingMaskIntoConstraints on the subviews, and then add constraints to position them, and finally add width and height constraints to the image view.
Using Auto Layout is also recommended if you ever intend to internationalize your app -- if you set up your constraints correctly, then the translated title will fit and you won't have to ever play around with hardcoded values.

Why the origin of contentview doesn't coincide the origin of scrollview when application setup?

The problem shows in the gif picture. (The view in yellow is the contentview. The view in black is the scrollview.)
The whole project code on Github.
Here is the code :
CGRect frame = CGRectMake( 50, 100, 200, 200);
UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:scrollView];
frame= CGRectMake( 0, 0, 500, 500);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500,500);
scrollView.backgroundColor = [UIColor blackColor];
myImageView.backgroundColor = [UIColor yellowColor];
scrollView.contentOffset = CGPointMake(0, 0)
One more problem shows in the picture:
Why the horizontal scrollbar is not at bottom of scrollview?
When I added a view between the scrollview and self.view the problem disappeared.
The code that I added:
UIView *view = [UIView new];
[self.view addSubview:view];
I found that the problem happened when I used UITabBarController.
Make the UIScrollView a global variable. Make the contentInset of the UIScrollView and UIScrollView's indicators UIEdgeInsetsZero when the views are loaded like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

Image not shown on vertical-scrollView in a background horizontal-scrollView

I would like to show let's say 3 long pictures (longer than iPhone's screen height) in a big background UIScrollView horizontally, and since each picture is longer than the screen height, so I put another 3 Sub-UIScrollView that are for vertical swiping in the background one.
I setup the background one like this:
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(screenWidth*_count, screenHeight);
_scrollView.pagingEnabled = YES;
_scrollView.alwaysBounceVertical = NO;
_scrollView.alwaysBounceHorizontal = YES;
And I setup each Sub-UIScrollView like this:
if (imageHeight > screenHeight) { //requiring extra scrollView to support
UIImageView *longImgView = [[UIImageView alloc] init];
[longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
longImgView.image = _image;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(originX, 0, screenWidth, screenHeight)];
[scrollView setContentSize:CGSizeMake(screenWidth, imageHeight)];
[scrollView setContentOffset:CGPointMake(originX, 0)];
scrollView.backgroundColor = [UIColor blueColor];
scrollView.alwaysBounceVertical = YES;
scrollView.alwaysBounceHorizontal = NO;
[scrollView addSubview:longImgView];
[_scrollView addSubview:scrollView];
}
Please notice that I set scrollView.backgroundColor = [UIColor blueColor]; to highlight this sub-scrollView, but when I run it, I can only see the blue-colored sub-scrollView, I cannot see the picture showing (longImgView.image = _image;).
UPDATE: I can see the memory address allocated to the _image with break point and console. Also I can see the sub-scrollView has set the same contentSize as the image size but only cannot see the image itself.
Wait: [longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
make that:
[longImgView setFrame:CGRectMake(0, 0, screenWidth, imageHeight)];

UIScrollView is not scrolling even with setting contentSize

My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.
I have a container view inside the scroll view that has a width of 960 points.
At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.
This is my viewDidLoad method:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView.scrollEnabled = YES;
[self.scrollView setFrame:CGRectMake(0,0,320,300)];
self.scrollView.contentSize = CGSizeMake(960, 300);
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
[containerView addSubview:subview1];
[self.scrollView addSubview:containerView];
}
Here is a sample code for create a scroll view programmatically:
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];
[scrollView addSubview:containerView];
So, there is no issue in your code, but something wrong with your storyboard configure.
I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.
Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?
If you can give me more information about this issue, I can help you.

CGRectMake( ) - y reference looks totally wrong

I am facing a really strange issue:
I am instantiating multiple UIImageView inside a for loop with the method CGRectMake, the y origin I am giving seems to be totally wrong on the screen:
Here is my code:
- (void)makeTheView
{
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];
header.backgroundColor = [UIColor colorWithRed:254/255.0f green:255/255.0f blue:213/255.0f alpha:1.0f];
[self.view addSubview:header];
for (int i = 0; i < 10; i++) {
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(5 + i * 75, 5, 70, 70)];
avatar.image = [UIImage imageNamed:#"bo_pic_baby5.jpg"];
[avatar.layer setCornerRadius:8.0];
avatar.layer.masksToBounds = YES;
NSLog(#"%f", avatar.frame.origin.y);
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, avatar.frame.size.width, 20)];
title.backgroundColor = [UIColor colorWithRed:148/255.0f green:148/255.0f blue:148/255.0f alpha:0.5f];
title.font = [UIFont fontWithName:#"Arial" size:15];
title.text = #"崔健";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[avatar addSubview:title];
[header addSubview:avatar];
}
}
According to this code avatar is within header at 5px from the top of header.
But the following is what I obtain visually:
note: when the white area begin, the header view stopped
This is not a real issue since I can reevaluate my frames like this :
CGRectMake(5 + i * 75, - 20, 70, 70)
but it looks really weird, and I am quite sure I am missing something totally trivial here...
I think this will be fixed by:
self.automaticallyAdjustsScrollViewInsets = NO;
Since iOS 7, view controllers automatically adjust scroll view insets so that the scroll view content is not hidden behind the navigation bar because it expects scroll views to start at the top of the screen.
However, the usual solution is to just set the scrollview frame.origin.y to 0.
Your Code is Absolutely Correct , As you are Adding the scrollview on (0,64) Position , So 64 will be count from Bottom of the Navigation Bar, If you want it on top (Just Below the Navigation bar), Change this declaration to as below :
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

Resources