UiimageView adapted in a uiview - objective-c

I have a little problem with my UIImageView.
I want the content of UIImageView don't overflow to UIView.
When I tryed this code, I have this result, but I won't a image in area striped.
UIView *view = [[UIView alloc] init];
[view setFrame:CGRectMake(10, 0, 300, 100)];
[self.view addSubview:view];
UIImageView *viewimageArticle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
viewimageArticle.image =[UIImage imageNamed:#"article1.jpeg"];
viewimageArticle.contentMode = UIViewContentModeScaleAspectFill;
viewimageArticle.frame = CGRectMake(0, 0, 320, 100);
[view addSubview:viewimageArticle];
I tryed a many properties, "contentMode" and "autorezingMask" but i don't have good result.
Thank you !

Use
view.clipsToBounds = YES;
This causes its subviews to be clipped to the receivers bounds.

set clipsToBounds = YES on view

I am not 100% clear of what you want to achieve, but what you want may be UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleAspectFill.
Also, you are creating the UIImageView frame in 300x100 first and resetting to 320x100. I believe the resetting part is not your intention.

Related

ScrollView is not working when try to add as a subview

I have problems with my scrollView! I have a view called Rootview and I wanna add three subviews to a scrollview. When I add the scrollview as a subview the middle view and the three subviews disappear. This is my code.. The Rootview (whole window) and the scrollview are outlets.
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 218, 2024, 218)];
[middleView setBackgroundColor: [UIColor yellowColor]];
[RootView addSubview:middleView];
//create new views for sap systems beginning from left
UIView *firstSystem1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 218)];
[firstSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:firstSystem1];
UIView *secondSystem1 = [[UIView alloc] initWithFrame:CGRectMake(305, 0, 300, 218)];
[secondSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:secondSystem1];
UIView *thirdSystem1 = [[UIView alloc] initWithFrame:CGRectMake(610, 0, 300, 218)];
[thirdSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:thirdSystem1];
CGSize scrollViewContentSize1 = CGSizeMake(2024, 218);
[middleScrollView setContentSize:scrollViewContentSize1];
[middleScrollView addSubview:middleView];
Can anyone help me with that?
The problem is that you aren't adding your middleScrollView anywhere
change this
[RootView addSubview:middleView];
for this
[RootView addSubview:middleScrollView];

Change Text Lable to Image in XCode

I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];

setting alpha of UITextField inputAccessoryView does not work

The inputAccessoryView is shown successfully, but it is totally black.
_textField = [[UITextField alloc] initWithFrame: CGRectZero]; // update frame later
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
inputAccessoryView.backgroundColor = [UIColor blackColor];
inputAccessoryView.alpha = 0.2;
_textField.inputAccessoryView = inputAccessoryView;
I am completely confused over this... can anyone explain why it happens?
Use This method of UIColor on previous line instead of setting alpha:-
[UIColor colorWithHue:saturation:brightness:alpha:];

Showing a Title and an Image Into the Table Header

I am trying to show into the header of my UITableViewController a title and under it an image illustrating the title. The next code (written into the viewDidLoad method) shows only the image, and this image over the rest of the table sections. How I can fix it to do what I want?
// Creates a header view.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
headerLabel.text = self.name;
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.backgroundColor = [UIColor clearColor];
GRect imageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *headerImage = [[UIImageView alloc] initWithFrame:imageRect];
UIImage *image = [UIImage imageNamed:self.imagePath];
[headerImage setImage:image];
headerImage.opaque = YES;
[containerView addSubview:headerLabel];
[containerView addSubview:headerImage];
self.tableView.tableHeaderView = containerView;
Thanks for reading.
Just adjust the views' frames accordingly. The container view's frame must be tall enough to contain the label and image view, and the image view's y coordinate must be set so that it is positioned below the label. The frame rects you are using in your code do not match.

Center & No repeat View.BackgroundColor (Iphone)

I put a background in my view like this:
UIImage *picture = [UIImage imageNamed:#"myImage.jpg"];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:picture];
The problem is my picture is not big enough for the screen, so it repeat many times and its also not center in the View. How can I change that?
Thank you,
If you want to use a background image like this, then use a UIImageView, rather than the backgroundColor property of a standard view. In Interface Builder, add a UIImageView, size it to the same size as your view (adjust the struts and springs to match), give it your image, and make your view transparent.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//view. =[UIImage imageNamed:#"15-tags.png"] ;//[UIColor redColor];
UIGraphicsBeginImageContext(view.frame.size);
int iImageWid=30;
int iImageHei=30;
CGRect rcCenter=CGRectMake(view.frame.origin.x+(view.frame.size.width-iImageWid)/2, view.frame.origin.y+(view.frame.size.height-iImageHei)/2, iImageWid, iImageHei);
[[UIImage imageNamed:#"15-tags.png"] drawInRect:rcCenter];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.backgroundColor = [UIColor colorWithPatternImage:image];