Constraints programmatically and with Storyboards - objective-c

It is possible to set constraints programmatically and while using Storyboards? I am using this https://github.com/raphaelschaad/RSPlayPauseButton as well as this for constraints https://github.com/SnapKit/Masonry. However I they are not being displayed correctly:
The image on the left is not being displayed correctly, and it not clickable (it is a button).
Relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
_playPauseButton = [[RSPlayPauseButton alloc] init];
_playPauseButton.tintColor = [UIColor blackColor];
[_playPauseButton addTarget:self action:#selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playPauseButton];
}
- (void)viewDidLayoutSubviews
{
[self.playPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view).with.offset(10);
}];
}

You need to adding width/height constraints for the playPauseButton, try this:
- (void)viewDidLoad {
[super viewDidLoad];
_playPauseButton = [[RSPlayPauseButton alloc] init];
_playPauseButton.tintColor = [UIColor blackColor];
[_playPauseButton addTarget:self action:#selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playPauseButton];
[playPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view).with.offset(10);
make.width.equalTo(#(50));
make.height.equalTo(#(50));
}];
}

Related

NavigationBar alpha value changed automatically after dismissing view controller

Weird thing
navigationbar'alpha value changes after dismissing view controller
Step
Click button that presents a new viewcontroller
Set navigation bar alpha to 0.0 when the button is clicked
Dismiss viewcontroller which I presented right before.
Result [NG]
navigationbar'alpha value is set to 1.0
Expected result
navigationbar's alpha should be 0.0 as expected
Below sample is pretty much easy
ViewController class
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *clickMe = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[clickMe setTitle:#"clickme" forState:UIControlStateNormal];
[clickMe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickMe addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickMe];
}
- (void)btnClicked {
self.navigationController.navigationBar.alpha = 0.0f;
second *sec = [[second alloc] init];
[self presentViewController:sec animated:YES completion:nil];
}
second class
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *getBack = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
[getBack setTitle:#"getBack" forState:UIControlStateNormal];
[getBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[getBack addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:getBack];
}
- (void)btnClicked {
[self dismissViewControllerAnimated:YES completion:nil];
}
I really would like to know what happened in this..
If someone knows this, let me know.. Thank you in advance.

UIButton as a subview in MKAnnotationView doesn't work

I have added a button to my annotation view, and it appears correctly. When the button is tapped, I want a call to be made, but when I tap the button, it doesn't respond (the button doesn't even highlight). It seems that the button doesn't receive the touch event, but I don't know why.
Here is my code in customAnnotationView.m
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(#"initWithAnnotation start");
//initialze the view
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(60.0, 85.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(30.0, 42.0);
}
//this has nothing to do with my question
self.title=[[[NSString alloc] init] autorelease];
self.description=[[[NSString alloc] init] autorelease];
self.phoneNumber=[[[NSString alloc] init] autorelease];
customAnnotation * tempCust = (customAnnotation *)annotation;
self.title=annotation.title;
self.description=tempCust.description;
self.phoneNumber=tempCust.phoneNumber;
//here is the question button
self.userInteractionEnabled=YES;
self.exclusiveTouch=NO;
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Call" forState:UIControlStateNormal];
//decide the button's position
CGRect rect=button.frame;
rect.origin.x=15;
rect.origin.y=47;
rect.size.width=40;
rect.size.height=20;
button.frame=rect;
[self addSubview:button];
[button becomeFirstResponder];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"initWithAnnotation finished");
return self;
}
//action method
-(void)buttonClicked:(id)sender{
NSString *callNumber=[[[NSString alloc] initWithFormat:#"tel://%#",self.phoneNumber] autorelease];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:callNumber]];
}
Why doesn't the button work?
self.rightCalloutAccessoryView = button;
Use the above instead of [self addSubview:button];
Hope this help's you

UIImagePickerController how to hide the flip camera button?

is there a way to hide the flip camera button inside the UIImagePickerController?
thanks for reading
!^_^!
I ended up using a custom subclass of UIImagePickerController to fix this (and other) issues:
#import "SMImagePickerController.h"
#implementation SMImagePickerController
void hideFlipButtonInSubviews(UIView *view) {
if ([[[view class] description] isEqualToString:#"CAMFlipButton"]) {
[view setHidden:YES];
} else {
for (UIView *subview in [view subviews]) {
hideFlipButtonInSubviews(subview);
}
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
hideFlipButtonInSubviews(self.view);
}
#end
You should be able to create an empty button inside an overlayview that you float on top of the flip camera button. I hacked the code below to test and it seemed to work. Give it a try.
UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(screenSize.width - 100.0f, 5.0f, 100.0f, 35.0f)];
[cameraOverlayView setBackgroundColor:[UIColor blackColor]];
UIButton *emptyBlackButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 35.0f)];
[emptyBlackButton setBackgroundColor:[UIColor blackColor]];
[emptyBlackButton setEnabled:YES];
[cameraOverlayView addSubview:emptyBlackButton];
cameraUI.allowsEditing = YES;
cameraUI.showsCameraControls = YES;
cameraUI.delegate = self;
cameraUI.cameraOverlayView = cameraOverlayView;

Issues with UISplitViewController and modal UIViewControllers

I have a really strange UI glitch in my iPhone/iPad app. Because I wanted to find the cause of it, I created a new project with as little code as possible. The important code is below.
Basically, I have a UISplitViewController containing two UIViewController subclasses. When a button is tapped, the first one presents modally and as UIModalPresentationFormSheet a UIViewController subclass called Modal. In there, when a button is tapped, another UIViewController subclass called Text is presented, this time as UIModalPresentationFullScreen. In Text, there is a UITextView. When it is tapped, everything is alright, but when the iPad is rotated, I get this:
The white part is the Text view controller, the red part in the background is ViewControllerTwo.
Does anybody have any idea why this happens? And what I can do to fix it?
Here is the Project: MediaFire
Here is the relevant source code:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewControllerOne *one = [[ViewControllerOne alloc] init];
ViewControllerTwo *two = [[ViewControllerTwo alloc] init];
UISplitViewController *split = [[UISplitViewController alloc] init];
split.viewControllers = [NSArray arrayWithObjects:one, two, nil];
self.window.rootViewController = split;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// ViewControllerOne.m
#import "Modal.h"
#implementation ViewControllerOne
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(30, 30, 44, 44);
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped
{
Modal *modalOne = [[Modal alloc] init];
modalOne.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalOne animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Modal.m
#import "Text.h"
#implementation Modal
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame = CGRectMake(30, 30, 44, 44);
buttonOne.tag = 1;
[buttonOne addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonTwo.frame = CGRectMake(30, 100, 44, 44);
buttonTwo.tag = 2;
[buttonTwo addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
[self.view addSubview:buttonTwo];
}
- (void)buttonTapped:(UIButton *)button
{
if (button.tag == 1)
{
Text *text = [[Text alloc] init];
text.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:text animated:YES];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Text.m
#implementation Text
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You may need to present the modal views from the UISplitViewController instead of it's subviews...
UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];
[splitViewController presentModalViewController:modal animated:YES];

How does autoresizing mask work?

I've found a lot of information on autoresizing but none has been able to solve my problem. I have a ViewController (RAViewController) who calls a view in its loadView method like so:
- (void)loadView
{
// Set Navigation Bar Title
self.navigationItem.title = #"RA";
// Add Background view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Add RAView
RAView *rAView = [[RAView alloc] initWithFrame:self.view.frame Controller:self];
[self.view rAView];
}
The view it calls (RAView) looks like this:
- (id)initWithFrame:(CGRect)frame Controller:(RAViewController *)Controller
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
controller = Controller;
// Draw Architecture View
[self drawArchitectureView];
}
return self;
}
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}
For some reason I can't get autoresizing mask to resize the button width when the device is landscape. Any help is greatly appreciated.
If you want it to keep the same height, but just stay fully centered, 10.0 from the left and right, try:
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}