How to center a logo on SideMenu using Xcode - ios

I want to Center a logo on the SideMenu and this is the code so far:
UIImageView *logo =[[UIImageView alloc] init];
logo.image=[UIImage imageNamed:#"menulogo"];
logo.contentMode = UIViewContentModeScaleToFill;
logo.layer.cornerRadius = cornerRadius;
logo.layer.masksToBounds = YES;
logo.frame = container.bounds;
[container addSubview:logo];
[headerView addSubview:container];

Create an extension to UIView
extension UIView {
func constraintToMidCenterXY(of view: UIView) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([centerXAnchor.constraint(equalTo: view.centerXAnchor),
centerYAnchor.constraint(equalTo: view.centerYAnchor))
}
}
Then you can do something like this
logo.constraintToMidCenterXY(of: container)
Also you should not set logo's frame to container's bounds! that's another problem
The above is a common approach that you can use, Autolayout. You can also use UIStackView

A good solution is to use Auto Layout to center your logo to the headerView. It will adapt accordingly to any iPhone screen size.

Related

iOS 8 Share Extension loadPreviewView

In iOS8 extensions, specifically the share extension, has anyone been able to override the loadPreviewView method to supply a custom preview view? I created one by returning a 50x50 UIView with a UIIMageView subview, but it shows up outside of the extension view (too far right) and it does not push the textView in the share extension over to the left. If anyone has customized this, please provide sample code! Thanks
Yep, I could get it working using width & height constraints. If I set a size and a mask it would not show up properly.
Here is a sample code:
- (UIView *)loadPreviewView {
UIView * previewView = [super loadPreviewView];
if (previewView == nil) {
previewView = [[UIImageView alloc] initWithFrame:CGRectZero];
[previewView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[previewView(90)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previewView)]];
[previewView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[previewView(90)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previewView)]];
}
return previewView;
}
Alternatively, you can derive from UIImageView and provide an implementation for intrinsicContentSize:
#interface MyImageView : UIImageView
#end
#implementation MyImageView
-(CGSize)intrinsicContentSize {
return CGSizeMake(70, 70);
}
#end
It's detailed in this WWDC session: https://developer.apple.com/videos/play/wwdc2015/224/?time=334
It's likely you'll also want to configure you view like so:
view.clipsToBounds = YES;
view.contentMode = UIViewContentModeScaleAspectFill;

CGRectMake doesn't work

I'm working on example from "iOS Programming Cookbook", based on iOS 7. I need to create UITextField and add it to ViewController.
In ViewController.m I have:
- (void) viewDidLoad {
[super viewDidLoad];
[self createField];
}
- (void) createField {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = #"Enter text to share";
self.textField.delegate = self;
[self.view addSubview:self.textField];
}
On screenshot from book textField appears in the middle of screen's width under the status bar, but when I run it textField appears on the top left corner of screen behind the status bar.
Maybe the problem is, that I run app on iPhone 6 simulator with iOS 8. But I don't know how to solve it.
Thanks!
Using self.textField.translatesAutoresizingMaskIntoConstraints = NO; is pretty much telling the object that it doesn't really care about the frame but relies more on the constraints that you give it. Setting it to YES takes the frame and automatically applies constraints to it to mimic the frame that you give it.
For example it would apply the constraints to have the frame appear at: CGRectMake(20.0f, 35.0f, 280.0f, 30.0f). When setting it to NO use NSLayoutConstraint and create the constraints programatically.
But in your case just remove the line
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
because it is set to YES by default.

UIImageView doesn't move

Just silly thing.
I placed UIImageView in the left top corner. Then make a new outlet
#IBOutlet var rocket : UIImageView
In the method ViewDidLoad I try to move this UIImageView with different properties but it doesn't
rocket.image = UIImage(named: "rocket.png")
rocket.frame = CGRectMake(200,200,200,200)
rocket.bounds = CGRectMake(200,200,200,200)
rocket.center = CGPointMake(400,400)
When I start the program an image in the UIImageView appears in the top left corner without any translation.
I guess you did not disables AutoLayout for your viewcontroller which is having that image.
please disable your Viewcontroller's AutoLayout property and change your code with following values so you can able to see the transformed image.
[rocket setFrame:CGRectMake(50, 50, 200, 200)];
rocket.bounds = CGRectMake(200,200,200,200);
rocket.center = CGPointMake(200,200);
[UIView animateWithDuration:0.3f animations:^{
rocket.image = UIImage(named: "rocket.png")
rocket.frame = CGRectMake(200,200,200,200)
rocket.bounds = CGRectMake(200,200,200,200)
rocket.center = CGPointMake(400,400)
}];

iOS: Autolayout causing UIScrollView to not scroll

I have set up a UIScrollView with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView which I have added on storyboard:
I have called the following method on -(void)viewDidLoad, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):
- (void)setupHorizontalScrollView
{
self.itemList.delegate = self;
[self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.itemList setBackgroundColor:[UIColor blackColor]];
[self.itemList setCanCancelContentTouches:NO];
self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.itemList.clipsToBounds = NO;
self.itemList.scrollEnabled = YES;
self.itemList.pagingEnabled = NO;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; tot++) {
if (tot==12) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];
CGRect rect = imageView.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
[self.itemList addSubview:imageView];
cx += imageView.frame.size.width;
}
[self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}
I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageViewI use to look as a navigation bar to the bottom of the screen.
I don't know what to do anymore, any help is appreciated :)
Try to set your scrollView's Content size int "viewDidLayoutSubviews" method with keeping the autolayouts set.
-(void)viewDidLayoutSubviews
{
[self.itemList setContentSize:CGSizeMake(required_width, required_height)];
}
Two Solutions:
Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another UIView to contain the UIScrollView to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.
Turn off Autolayout, and change the autoresize masks of your UIImageView to be what you wish.
Insert: [scrollView setContentSize:CGSizeMake(x,y)]; in the following method:
-(void)viewWillAppear:(BOOL)animated

Popup displaying image on iPhone app?

I want to show an image in popup view which also contains the close button.
I have searched for that but I did not found any thing about that.
Please help me??
Just try out following simple way -
You can create one UIView that contains UIImageView and UIButton.
Whenever you want to show the the image just assign image to the UIImageView and and show the UIView.
And when you want to hide the the image just hide the UIView on pressing UIButton (i.e close button).
Hope this will help you.
try this for popup
1) ALPopup
2) BlureModelView
3) Ogactionchooser
4) Other and This
- (IBAction)btn:(id)sender {
UIView *contentView;
UIImageView *dot;
contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,350)];
contentView.backgroundColor = [UIColor whiteColor];
contentView.layer.cornerRadius = 5.0;
dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,40,200,200)];
dot.image = [UIImage imageNamed:"DSC_0055.JPG.jpg"];
dot.contentMode = UIViewContentModeScaleAspectFit;
[contentView addSubview: dot];
[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];
}
}
https://github.com/kgn/KGModal

Resources