UIView - UIScrollView odd behaviour - ios

I have a UIViewController named MenuViewController in which i create an UIView MessageView and pass it a NSString:
MessageView *mess = [[MessageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
[mess messageString:#"A:text\nM:text\nA:text\nM:text"];
[self.view addSubview:mess];
In MessageView i am creating an UIScrollView and several UILabels; my problem is the following and i can't understand the behaviour:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(540, 50, 360, 160)];
scrollView.backgroundColor = [UIColor redColor];
[self addSubview:scrollView];
}
return self;
}
- (void)messageString:(NSString *)string
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
>>>>> HERE is the problem <<<<<<
if i try to add label to scrollView it does not appear
[scrollView addSubview:label];
if i try to add label to self it appears
[self addSubview:label];
}
I would like to create all of the content i would like to show here in MessageView and just call it from the view controller. Does anybody have an idea of what may be the problem ?

This line:
UIImageView *label = [UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
Should be producing an error and a warning. For one, you're missing a starting bracket [, and on top of that, you're creating a pointer to a UIImageView object, and then assigning a label to it. Try again with the line written out like this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
This is of course assuming that the method is even being called, and the scroll view is non-nil and has a proper frame when you try to access it.

You are adding the label outside of scrollview's visible frame: Replace
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
with
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, scrollView.frame.size.width, scrollView.frame.size.height/6)];

Related

Text with different colored background

How to achieve the below design in iOS , is any controls available to achieve this. I am not that much experienced to achieve this design in native . Is it developed in native or kind of hybrid technology is used
Please find the below image for reference
I made a programmatic Example (Kind of "Hello World") for you
(it is programatic for a reason, that I believe it is easier to understand how things are working.
It is not perfect solution it can be done better, (performance wise) but I choose it as a simple example to get you started (Personally I would use CALayer for the backgrounds).
so here:
make a new class as follows:
MyView.h
#import <UIKit/UIKit.h>
#interface MyView : UIView
#end
MyView.m
#import "MyView.h"
#implementation MyView
- (instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
self.backgroundColor = [UIColor grayColor];
//This rect is in self coordinate system
UIView * viewPurple = [[UIView alloc]initWithFrame:CGRectMake(0, 200, frame.size.width, 50)];
viewPurple.backgroundColor = [UIColor purpleColor];
[self addSubview:viewPurple];
//This rect is in viewPurple's coordinate system
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label setText:#"Hello World!"];
[label setTextAlignment:(NSTextAlignmentCenter)];
[label setTextColor:[UIColor whiteColor]];
[viewPurple addSubview:label];
//All the frame's rects are in it's `superView`/ parent 's coordinate system.
UIView * viewGreen = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2, 200, frame.size.width, 50)];
viewGreen.backgroundColor = [UIColor cyanColor];
[self addSubview:viewGreen];
UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label2 setText:#"World Hello!"];
[label2 setTextAlignment:(NSTextAlignmentCenter)];
[label2 setTextColor:[UIColor whiteColor]];
[viewGreen addSubview:label2];
}
else
{
//Can't allocate the UIView - quite a rare problem.
}
return self;
}
#end
#import "myView.h" in your view controller
and add this snippet to the - (void)viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyView * view = [[MyView alloc]initWithFrame:self.view.frame];
[self.view addSubview:view];
}
the result will be as follow

How to Call the Custom UIView from ViewController using Frame Size

I want to show the CustomUIView From my ViewController.How to call using frame?I am getting Confused in Frame as am a newbie.
My Theme is,I Want to show the LoginViewKarnataka and usernameLabel in my ViewController in the y Value 150.
This is My Code
ViewController.m
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
LoginViewKarnataka(CustomUIView)
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(#"frame==>>%f",frame);
if (self)
{
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
}
}
Change your viewController code to
LoginViewKarnataka *loginView = [[LoginViewKarnataka alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 150)];
[self.view addSubview:loginView];
In your LoginViewKarnataka view
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor redColor]];
UILabel *usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
[usernameLabel setText:#"username"];
[usernameLabel setTextColor:[UIColor blackColor]];
[self addSubview:label];
}
return self;
}
In your above code, you are adding a label at the positions of x: 20, y: 20.
To print frame of any view use following code.
NSLog(#"frame : %#",NSStringFromCGRect(self.view.frame));
To print size of any view
NSLog(#"frame : %#",NSStringFromCGSize(self.view.frame.size));
Your code is fine. All that was missing was adding usernameLabel as a subview to your custom view.
[self addSubview:usernameLabel];
P.S. If you need to log any frame values then you can simply log the view. The frame value is printed in the description of the view. Also you can use DCIntrospect for UI debugging if you have created any complex UI.
Thanks.

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.

UIScrollView setContentOffset will add a subview to the scrollview?

I happened to find that setContentOffset to a UIScrollView will cause the scrollView append a new view to its subviews. here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((320 - 261) / 2, 50, 261, 67)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.backgroundColor = [UIColor darkGrayColor];
for (int i = 0; i < 5; i ++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 87, 1, 87, 65)];
view.tag = i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:#"%d", i];
[view addSubview:label];
view.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view];
}
scrollView.contentSize = CGSizeMake(87 * 5, 67);
NSLog(#"before scroll: count of subview is %d", scrollView.subviews.count);
CGPoint offset = CGPointMake(87, 0);
[scrollView setContentOffset:offset animated:YES];
NSLog(#"after scroll: count of subview is %d", scrollView.subviews.count);
[self.view addSubview:scrollView];
}
Before calling setContentOffset:offset, the number of subviews of the scrollView is 5. This is what I expect. After that, the number turns to be 6.
Is it working as designed? How to avoid the new subview appended?
The extra subview is the scroll indicator. If you check again in a later method, it will have gone away. Don't worry.
You can confirm this by hiding the scroll indicators (showsVerticalScrollIndicator and showsHorizontalScrollIndicator properties).
Don't try to assume things about the view hierarchy of UIKit classes. UIKit can, and does, add its own views to several things - see tableviews and their cells, a navigation controller's view and so on.

how can i put the ScrollView on ViewController

I am trying to put the Scrollview on the viewcontroller? is it possible? and i also want to put the UIView on the ScrollView.
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(20, 60, 280, 200)];
scrollView.contentSize = CGSizeMake(280, 200);
scrollView.backgroundColor=[UIColor orangeColor];
scrollView.delegate = self;
[self.view addSubview:scrollView];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 280, 200)];
container.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:container];
In this code i tried to put the scrollview on the VC and also i tried to put View on the ScrollView.
rb1 = [[RadioButton alloc] initWithGroupId:#"first group" index:j];
rb1.frame = CGRectMake(20,dy,22,22);
[container addSubview:rb1];
[rb1 release];
label1 =[[UILabel alloc] initWithFrame:CGRectMake(50, dy, 60, 20)];
label1.backgroundColor = [UIColor clearColor];
label1.text = [answers objectAtIndex:j];
[container addSubview:label1];
and here i tried to put the a radio button on View.
I hope it is clear:) Sorry my bad english.Thanks.
Scroll occurs when the frame of a scroll view is less than your content size.
Your frame is having size (280,200) and you have provided same size to contentSize.
To make the scroll possible replace below line into your code.
scrollView.contentSize = CGSizeMake(300, 260);
Here you put the same content size as scroll size:
scrollView.contentSize = CGSizeMake(280, 200);
In content size you should enter the size of area you want to scroll. In your case scroll does not need to scroll because scrollable area is same as scrollView size.
try this to put the scrollview on the controller
#import <UIKit/UIKit.h>
#interface YourClass : UIViewController {
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#end
to put v/v try this
NSUInteger scrollContentCount = 0;
for (NSUInteger arrayIndex = 0;
arrayIndex < [contents count];
arrayIndex++) {
// set scorllview properties
CGRect frame;
frame.origin.x = self.mScrollView.frame.size.width * arrayIndex;
frame.origin.y = 0;
frame.size = self.mScrollView.frame.size;
myScrollView.autoresizingMask = YES;
// alloc - init PODetailsView Controller
myController = [[MyController alloc] initWithNibName:#"MyController" bundle:[NSBundle mainBundle]];
[myController.view setFrame:frame];
// add view in scroll view
[self.myScrollView addSubview:myController.view];
scrollContentCount = scrollContentCount + 1;
}
// set scroll content size
self.myScrollView.contentSize =
CGSizeMake(self.myScrollView.frame.size.width * scrollContentCount,
self.myScrollView.frame.size.height);
}

Resources