Custom View AddSubview programmatically, not shown UIElements… - ios

I do this to add a Subview from Storyboard, which is a subview from a ViewController with its own Class. (TomStatusbarController) ...it is set as CustomClass in Storyboard. I do also Constraints with Layoutformats. This works fine, but the Statusbar is just red and i see no UIElements. I get lots on constraints warnings but it worked before, when i had it as an IBOutlet, but I have to change this now.
-(void)viewDidLoad {
[super viewDidLoad];
if (self) {
self.tomCaptureStatus = [[TomStatusbarController alloc] initWithFrame:CGRectMake(0, 0, 375, 78)];
self.tomCaptureStatus.layer.bounds = CGRectMake(0, 0, 375, 78);
self.tomCaptureStatus.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
self.view.translatesAutoresizingMaskIntoConstraints = YES;
self.tomCaptureStatus.backgroundColor = [UIColor redColor];
[self.view addSubview:self.tomCaptureStatus];
[self.view superview];
}
}
So why i don't see any thing, expect my backgroundcolor?
Tom

Related

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;
}

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.

UIView - UIScrollView odd behaviour

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)];

UIScrollView scrolling automatically by 64 points

I'm adding a UIScrollView to a UIViewControllers view. For some reason, between adding the scroll view to the view and it getting displayed, the contentOffset is set to {0, -64}, 64 being the status bar's 20 plus the navigation bar's 44 points (I guess). Below is some code that reproduces the issue, and an image.
How do I prevent iOS from setting the contentOffset?
- (void)viewDidLoad
{
[super viewDidLoad];
_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
_scroll.backgroundColor = [UIColor greenColor];
_scroll.delegate = self;
UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
red.backgroundColor = [UIColor redColor];
[_scroll addSubview:red];
[self.view addSubview:_scroll];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// outputs {0, -64}
NSLog(#"%#", NSStringFromCGPoint(_scroll.contentOffset));
}
Set automaticallyAdjustsScrollViewInsets on your view controller to NO, otherwise it'll adjust insets on the first subview of it's root view that happens to be of UIScrollView class.
More on this in iOS 7 Transition Guide.
For iOS 11+, set UIScrollView's contentInsetAdjustmentBehavior = .never to prevent the system from adjusting contentOffset automatically when it's being added to another view

Subview of custom UIButton class doesn't trigger click

I'm writing a custom UIButton subclass, to create my own fully customized button
to have total controll. Still, I'm having problems when I try to add a subview
to that UIButton's view. The subview blocks the "touch" events so it won't bubble to
the UIButton itself..
This is a demonstration:
I first create my CustomNavigationButton, with a frame.. It's magenta.
I can see the magenta on the screen so it's there. Secondly, I add a subview
to that CustomNavigationButton (which is green), I can see the green so it's
there, but if I click the green rectangle (subview), the "UIControlEventTouchUpInside"
doesn't get called on my CustomNavigationButton..
In my AppDelete:
CustomNavigationButton* mapBtn = [[CustomNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
mapBtn.backgroundColor = [UIColor magentaColor];
[mapBtn addTarget:self action:#selector(goMapHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapBtn];
And here's my CustomNavigationButton class (which is a subclass of UIButton)
#implementation CustomNavigationButton
#synthesize bg;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// STORE INITIAL FRAME
self.initialFrame = frame;
bg = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
bg.backgroundColor = [UIColor greenColor];
[bg setUserInteractionEnabled:YES];
[self addSubview:bg];
}
return self;
}
#end
I figured out how to do it!
If "bg" is the subview I'm adding to the UIButton, then you should do:
bg.userInteractionEnabled = NO;
bg.exclusiveTouch = NO;
But keep in mind that, if your subview extends the frame of the UIButton,
a touch will not occur! You can check if your subview exceeds the UIButton
by giving the UIButton a background-color.

Resources