Custom layout on iOS 7 renders element to high - layout

I have a problem with custom layout on iOS 7. Everything appears too high, i.e. under the navigation bar. On iOS 6 it works like a charm even when compiled with iOS 7 SDK.
Below I post code used to init and layout the view. I don't use Interface Builder and I would prefer not to. Any help appreciated.
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.documentTitle = [UITextField new];
self.documentExtension = [UILabel new];
self.documentTitle.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Document title"];
self.documentTitle.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 1];
self.documentTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.documentTitle.borderStyle = UITextBorderStyleRoundedRect;
self.documentTitle.returnKeyType = UIReturnKeyDone;
self.documentTitle.keyboardType = UIKeyboardTypeDefault;
self.documentExtension.text = #".pdf";
self.documentExtension.backgroundColor = [UIColor clearColor];
[self addSubview:self.documentTitle];
[self addSubview:self.documentExtension];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
UIEdgeInsets padding = UIEdgeInsetsMake(12, 10, 12, 10);
CGRect layoutRect = UIEdgeInsetsInsetRect(self.bounds, padding);
CGPoint p = layoutRect.origin;
CGSize tmpSize;
CGFloat tfSpaceHorizontal = 6;
CGFloat tfSpaceVertical = 6;
CGSize tmpSizeDocumentExtension = [self.documentExtension sizeThatFits:CGSizeMake(CGRectGetWidth(layoutRect), 1000)];
tmpSize = CGSizeMake(
roundf((CGRectGetWidth(layoutRect) - tfSpaceHorizontal - tmpSizeDocumentExtension.width)),
32
);
self.documentTitle.frame = CGRectMake(
p.x, p.y,
tmpSize.width,
tmpSize.height
);
p.x += tmpSize.width + tfSpaceHorizontal;
CGFloat addedHeight = roundf(tmpSize.height/2 - tmpSizeDocumentExtension.height/2);
addedHeight = addedHeight > 0 ? addedHeight : 0;
p.y += addedHeight;
self.documentExtension.frame = CGRectMake(
p.x,
p.y,
tmpSizeDocumentExtension.width,
tmpSizeDocumentExtension.height
);
}

In iOS 7, every view controller uses full-screen layout, so the coordinate system starts under the status bar, so mind that while positioning your views.
This is mentioned in Apple's iOS 7 UI Transitioning Guide.

Related

how to get userintraction in uiwebview when added as sub view on a button in ios

I have a button performs some action, have added uiwebview as a sub view of the button to load a small gif animation, I can tap the button to perform the task but I needed to perform the same action when I tap the web view.
-(void)numeritsIphone4{
arrayButtonNames = #[#"Button1.png",#"Button2",#"Button3",#"Button4",#"Button5.png",#"Button6.png",#"Button7.png",#"Button8.png",#"Button9.png",#"Button10.png"];
float y = 20;
int x = 3;
int count = 0;
for (int i = 0 ; i < 10; i++)
{
count ++;
buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonsarray[i].frame = CGRectMake(x, y, 155, 90);
buttonsarray[i].tag = i;
[buttonsarray[i] setBackgroundImage:[UIImage imageNamed:[arrayButtonNames objectAtIndex:i]] forState:UIControlStateNormal];
[buttonsarray[i].titleLabel setFont:[UIFont fontWithName:#"Verdana" size:80]];
[buttonsarray[i] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//buttonsarray[i].titleLabel.textAlignment = UITextAlignmentLeft;
buttonsarray[i].contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[buttonsarray[i] setTitle:[NSString stringWithFormat:#"%d",digit] forState:UIControlStateNormal];
digit ++;
character = [[UIWebView alloc]initWithFrame:CGRectMake(100, 30, 25, 25)];
NSURL *url=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Star" ofType:#"gif"]];
character.opaque = NO;
character.backgroundColor = [UIColor clearColor];
character.scrollView.scrollEnabled = NO;
[character loadRequest:[NSURLRequest requestWithURL:url]];
character.userInteractionEnabled = YES;
[buttonsarray[0] addSubview:character];
[character release];
[url release];
[buttonsarray[i] addTarget:self action:#selector(performOperation4:) forControlEvents:UIControlEventTouchUpInside];
x = x + 159;
[self.view addSubview:buttonsarray[i]];
if(count == 2)
{
count = 0;
x = 3;
y = y+ 92;
}
}
A possibility is to add a UITapGestureRecognizer to your UIWebView control and call the same function when the gesture recognizes.
NOTE: This way you have limitation if you have some buttons, links etc on your UIWebView page, that button, links might not get trigger. So give it a try and see if it helps you.

iOS 7 Views reposition under status bar using topLayoutGuide

I have seen many confusing and heck solutions to deal with status bar in iOS 7 but I liked following solution as mentioned by Apple at - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926
....but it doesn't work! This method gets called as expected but childView is not moving down 20 pixels.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
UIView *childView = self.childController.view;
id topGuide = self.topLayoutGuide;
CGFloat topBarOffset = self.topLayoutGuide.length; //I checked this valued is 20
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (childView, topGuide);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: #"V:[topGuide]-20-[childView]"
options: 0
metrics: nil
views: viewsDictionary
]
];
}
}
Following works for me but I would really like to solve this problem using AutoLayout using topLayoutGuide.
- (void)layoutSubviews
{
[super layoutSubviews];
self.backgroundColor = [UIColor blackColor];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect frame = self.bounds;
frame.origin.y = 20;
frame.size.height -= 20;
self.childView.frame = frame;
} else {
self.childView.frame = self.bounds;
}
}
Any ideas why using topLayoutGuide not working? Thanks.

start at the top of the page when changing page

ı have a problem about scrollview.
my project about a newspaper.my problem about : I want to start at the
top of the page when changing page.
scrollview have 8 news.ı want to beginning at the top of the text on
the page when change 2. or 3. or.. news..
please help me about it.because ı need to deliver the project on Monday.for this reason ı
must do today or tomorrow.
code of project in here
myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
myScrollview.pagingEnabled = YES;
myScrollview.scrollEnabled =YES;
myScrollview.clipsToBounds = NO;
myScrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollview.showsHorizontalScrollIndicator = YES;
myScrollview.backgroundColor = [UIColor blackColor];
myScrollview.delegate = self;
NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"photo1.png"],[UIImage imageNamed:#"photo2.png"],[UIImage imageNamed:#"photo3.png"],[UIImage imageNamed:#"photo4.png"],nil];
for (int i = 0; i <viewcount; i++)
{
CGFloat x = i * self.view.frame.size.width;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[subView setBackgroundColor:[UIColor blackColor]];
[subView setCanCancelContentTouches:NO];
subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
aImageView.frame=CGRectMake(0, 0, 320, 900);
[aImageView setTag:viewcount];
[subView addSubview:aImageView];
[subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
subView.minimumZoomScale = 1;
subView.maximumZoomScale = 9;
subView.delegate = self;
[subView setScrollEnabled:YES];
subView.contentSize = aImageView.frame.size;
[myScrollview addSubview:subView];
}
myScrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);
[self.view addSubview:myScrollview];
https://biliyomusunuz.com

resize UIImage in table cell programmatically

how can i resize the UIImage in table cell
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.frame=CGRectMake(0, 0, 20, 30);
cell.imageView.image =[UIImage imageNamed:#"Diseases.png"];
it is not working
Use this method, pass the size and image, and get the image then show in cell
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You can also make your custom UITableViewCell, and design as you want to look it
This worked for me.
///////////////////////////////// TO RESIZE IMAGE //////////////////////////////////
////////// FIRST WRITE DESIRED SIZE AND CALL BELOW FINCTION ///////
CGSize newSize ;
newSize.width = 30;
newSize.height = 30;
UIImage *imgSelectedNew = [self imageWithImage:savedImage scaledToSize:newSize];
- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSize:(CGSize)targetSize
{
//UIImage *sourceImage = sourceImage;//= self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil)
NSLog(#"could not scale image");
return newImage ;
}
well m also n indorian , Gud to see the other one !!! :)

cocos2d box2d scale sprite -> removechild (how?)

I am little slow in English, please do understand.
Here is my source code:
- (void)createBall:(CGPoint)touchedAt{
CGSize winSize = [CCDirector sharedDirector].winSize;
ball2 = [CCSprite spriteWithFile:#"Ball.png" rect:CGRectMake(0, 0, 54, 54)];
ball2.position = ccp(touchedAt.x,touchedAt.y);
[self addChild:ball2];
b2BodyDef ballBodyDef2;
ballBodyDef2.type = b2_dynamicBody;
ballBodyDef2.position.Set(touchedAt.x/PTM_RATIO, touchedAt.y/PTM_RATIO);
ballBodyDef2.userData = ball2;
b2Body *body2 = _world->CreateBody(&ballBodyDef2);
b2CircleShape circle;
circle.m_radius = 89.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
b2FixtureDef ballShapeDef2;
ballShapeDef2.shape = &circle;
ballShapeDef2.density = 1.0f;
ballShapeDef2.friction = 0.2f;
ballShapeDef2.restitution = 0.8f;
body2->CreateFixture(&ballShapeDef2);
}
-(void)createBall2
{
CGSize winSize = [CCDirector sharedDirector].winSize;
globalSprite = [CCSprite spriteWithFile:#"Ball.png"];
globalSprite.position = ccp(winSize.width/2 + globalSprite.contentSize.width, winSize.height/2);
[self addChild:globalSprite];
b2BodyDef ballBodyDef3;
ballBodyDef3.type = b2_dynamicBody;
ballBodyDef3.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
ballBodyDef3.userData = globalSprite ;
b2Body *body3 = _world->CreateBody(&ballBodyDef3);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
b2FixtureDef ballShapeDef3;
ballShapeDef3.shape = &circle;
ballShapeDef3.density = 1.0f;
ballShapeDef3.friction = 0.2f;
ballShapeDef3.restitution = 0.8f;
body3->CreateFixture(&ballShapeDef3);
}
// initialize your instance here
-(id) init
{
if( (self=[super init])) {
// enable touch
// enable accelerometer
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isAccelerometerEnabled = YES;
self.isTouchEnabled = YES;
// Create sprite and add it to the layer
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
// Create ball body and shape
[self schedule:#selector(tick:)];
//[self schedule:#selector(gameLogic:) interval:1.0];
[self createBall2];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
[self createBall:location];
}
- (void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
I want to
touch -> sprite create(circle) -> sprite scale -> sprite remove
but
- (void)tick:(ccTime) dt <---------- this is simulator turn off!
I want to way
Try this :
world->DestroyBody(sprite);

Resources