Why UITableView works but UIView doesn't - ios

I'd like to pop an view with textInput box and button. I can't understand why UITableView works but UIView doesn't.
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerView = [[UITableView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
[[self view]addSubview:self.containerView];
}
Use above code I can saw the view. But if I change to:
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
[[self view]addSubview:self.containerView];
}
I will get nothing, a black screen. Nothing displayed.
Could someone tell me the reason? Thank you very much.

self.containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
self.containerView.backgroundColor = [UIColor redColor];
try this code
.

Related

ScrollView image addSubview issue

I'm trying to add an imageView in the ScrollView but I always get the ScrollView empty. Please where would be my issue? While I have added the delegate method <UIScrollViewDelegate> also made the connection in the storyboard.
My code:
- (void) viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void) slider {
_scrlView = [[UIScrollView alloc]init];
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
First, you don't add the scrollView to any view, so it won't be presented on screen.
Second, this:
_scrlView = [[UIScrollView alloc]init];
creates a scrollView with CGRectZero which is the same like writing:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
This means the contentSize is also CGRectZero.
Edit: If you don't want to override the creation of the scrollView (because you have already set it in Interface Builder), remove the line _scrlView = [[UIScrollView alloc]init];.
But if you are using Auto Layout in the storyboard you should call [self slider] in viewDidLayoutSubviews.
You need to set frame first, for getting its width and height,
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(YOUR_X, YOUR_X, YOUR_WIDTH, YOUR_HIEGHT)];
// do this if you have not added Scrollview in storyboard, if Added, then no need to invoke, init by line [[UIScrollView alloc] init] ;
...
...
[self.view addSubview:_scrlView];
}
I would suggest one more thing, to call your slider method from viewDidAppear method of UIView
Enjoy Coding !!
You need to allocate the ScrollView if have not used a storyboard. Also in that case, specify a frame:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
You are reallocating the imageviews, try to set image after allocating it once.
Another thing : Dont init scrollview again.
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
[_img1 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img2 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img3 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
My reply would be
if you are not using Storyboard or Interface builder , also autolayout is unchecked, set the frame of uiscrollview after its allocation and use alloc init only one time for one image. Add image on scrollview and scrollview on superview
if you are not using Storyboard or Interface builder and autolayout is checked, then put proper constraints on scrollview and imageviews. Read this
https://developer.apple.com/library/ios/technotes/tn2154/_index.html

UITableView header view bug?

I have next issue:
this code worked well:
//UITableViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
[v setBackgroundColor:[UIColor redColor]];
self.tableView.tableHeaderView = v;
}
Result:
But next code is not worked:
// TopViewController class
#interface TopViewController : UIViewController
#end
// UITableViewController class
#property (strong) TopViewController *topBar;
#synthesize topBar;
- (void)viewDidLoad
{
[super viewDidLoad];
topBar = [[TopViewController alloc] init];
topBar.view.frame = CGRectMake(0, 50, 320, 80);
[topBar.view setBackgroundColor:[UIColor greenColor]];
self.tableView.tableHeaderView = topBar.view;
}
Result - just white space
But when i do this:
topBar.view.frame = CGRectMake(0, 50, 320, 400); // change height to 400
I've got next result:
Can some one explain this strange behavior?

UIScrollView being clipped when added in viewWillAppear

I have a subclass of UIScrollView and I am trying to add it to my view. For some reason when I add it in the viewDidLoad it works as expected. But when I try adding it in the viewWillAppear then it seems to change the contentSize (although when I print the contentSize it is 905 in either case)
This works as expected:
- (void)viewDidLoad
{
[super viewDidLoad];
diptic = [[DipticView alloc] initWithFrame:self.view.frame];
diptic.item = self.item;
[self.view addSubview:diptic];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
This cuts off the scrollView (both views are scrolled down all the way.)
- (void)viewDidLoad
{
[super viewDidLoad];
diptic = [[DipticView alloc] initWithFrame:self.view.frame];
diptic.item = self.item;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.view addSubview:diptic];
}
Any ideas on why this might be happening?
EDIT:
I think the frame is being reset somewhere because if I do :
diptic = [[DipticView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
I get the same results...

UITapGestureRecognizer EXC_BAD_ACCESS in self-contained UIView subclass

I've used UITapGestureRecognizer tons of times, but in this case I'm getting EXC_BAD_ACCESS when a tap occurs. I think it has to do with the fact that I'm adding it to an Alert-type overlay view. And for some reason the overlay view isn't getting retained even though it's on-screen.
I'm creating the view like this:
HelperView *testHelper = [HelperView helperWithBodyText:#"test text"];
[testHelper presentAtPoint:screenCenter];
The convenience method in HelperView.m looks like this:
+ (id)helperWithBodyText:(NSString*)text
{
return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}
And the rest of the code looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.overlay.backgroundColor = [UIColor redColor];
self.overlay.alpha = 0.6;
[self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissMe:)];
[self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped
self.content = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
self = [self initWithFrame:frame];
if (self) {
//TEST frame
self.content.frame = CGRectMake(0, 0, 217, 134);
// Initialization code
UIImage *bgImage = [[UIImage imageNamed:#"helper-bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
bgImgView.bounds = self.content.frame;
[self.content addSubview:bgImgView];
}
return self;
}
- (void)presentAtPoint:(CGPoint)loc
{
CGPoint newPoint = [self.container convertPoint:loc toView:self.container];
self.content.center = newPoint;
[self.container addSubview:self.content];
}
- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
//this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
HelperView is not the view getting displayed and it's not getting retained, but you're using as the target for the gesture recognizer. The AGWindowView property "container" is getting displayed and retained by its superview. Your code needs refactoring since you have this view HelperView that doesn't ever display anything itself, but if you want it to work like this you need to retain HelperView so it doesn't automatically get released. You can do this by assigning it to a strong instance variable.

Show view from xib in ScrollView

firstly I want to say that I am newbie at iOS development. I have a problem with showing a view from xib file in ScrollView.
This MyView is only green background with one label. Here is a code from its controller:
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//init code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
}
return self;
}
#end
And here is a code from main controller where is the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.frame = CGRectMake(0, 20, 320, 350);
scroll.contentSize = CGSizeMake(320, 350);
scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroll.bounces = YES;
scroll.bouncesZoom = YES;
scroll.scrollEnabled = YES;
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 5.0;
scroll.delegate = self;
scroll.pagingEnabled = YES;
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,20,300,300);
[scroll addSubview:myView];
}
Only thing I want to do is that I want to see MyView (green background with one label) inside the ScrollView. If I run this app, scrollview is empty, nothing is shown inside.
Probably it is a stupid question but as I said, I am newbie at this. I have a PageControll app from Apple but for me this is quite complicated code in my iOS beginning. Of course I was googling... Please tell me where is a problem. Thank you
Where do you define scroll? Is it a property of your viewcontroller?
I think the problem here is that you don't add scroll view to your viewcontroller's view with
[self.view addSubview:scroll]
Everything seems to right. Just retain the view in your class that you are receiving from the following line and add that as a subview to your scrollview.
self.myView = [nibContents objectAtIndex:0];
Best Regards.

Resources